我试图创建一个回购计算器,当用户正常购买一些东西,当订单完成时,用户可以用SKU进行回购,当回购完成时,会有一定的金额扣除,但剩余的金额将进入钱包.当用户试图购买新产品时,钱包金额应该被扣除,我能够扣除金额,但在数据库中,金额没有被扣除.

// Function to create the buy_back_calculator table
function create_buy_back_calculator_table()
{
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate();
    $table_name = $wpdb->prefix . 'buy_back_calculator';
    $total_wallet_amount = $wpdb->prefix . 'total_wallet_amount';

    // SQL statement to create the buy_back_calculator table
    $sql = "CREATE TABLE $table_name (
        id INT AUTO_INCREMENT PRIMARY KEY,
        sku VARCHAR(255) NOT NULL,
        user_name VARCHAR(255) NOT NULL,
        user_email VARCHAR(255) NOT NULL,
        user_billing_address VARCHAR(255) NOT NULL,
        user_phone_number VARCHAR(20) NOT NULL,
        date_of_product_purchase DATE NOT NULL,
        date_of_buyback DATE NOT NULL,
        product_price DECIMAL(10, 2) NOT NULL,
        number_of_days INT NOT NULL,
        buy_back_price DECIMAL(10, 2) NOT NULL,
        amount_in_wallet_remaining DECIMAL(10, 2) NOT NULL,
        transit_status VARCHAR(255) NOT NULL,
        image_path VARCHAR(255) NOT NULL
    ) $charset_collate;";

    // Execute the SQL query to create the buy_back_calculator table
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);

    // SQL statement to create the total_wallet_amount table
    $sql = "CREATE TABLE $total_wallet_amount (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_email VARCHAR(255) NOT NULL,
        total_wallet_amount DECIMAL(19, 4) NOT NULL
    ) $charset_collate;";

    // Execute the SQL query to create the total_wallet_amount table
    dbDelta($sql);
}

我try 了下面的功能,但它是从钱包中扣除 checkout 总价,而不是更新数据库中的total_wallet_amount.

function adjust_order_total_with_deduction()
{
    if (is_user_logged_in()) {
        global $wpdb;
        $current_user = wp_get_current_user();
        $user_email = $current_user->user_email;
        $total_wallet_amount_table = $wpdb->prefix . 'total_wallet_amount';
        $user_remaining_sum = $wpdb->get_var($wpdb->prepare("SELECT total_wallet_amount FROM $total_wallet_amount_table WHERE user_email = %s", $user_email));

        $deduction_amount = $user_remaining_sum; // Fixed amount to deduct
        if ($deduction_amount > 0) {
            WC()->cart->add_fee(__('Deduction Amount', 'your-text-domain'), -$deduction_amount);
        }
    }
}
add_action('woocommerce_cart_calculate_fees', 'adjust_order_total_with_deduction');

推荐答案

下面重新访问了代码,它将从购物车中推断 checkout 时可用用户钱包的总金额.然后,当下单时,当订单状态设置为正在处理或已完成时,将从用户钱包中删除推断的金额:

// Utility function: Get user wallet amount
function get_total_wallet_amount( $user_email ) {
    global $wpdb;
    return $wpdb->get_var($wpdb->prepare("
    SELECT total_wallet_amount FROM {$wpdb->prefix}total_wallet_amount WHERE user_email = %s
    ", $user_email));
}

// Utility function: Update user wallet amount
function update_total_wallet_amount( $user_email, $new_total ) {
    global $wpdb;
    return $wpdb->query($wpdb->prepare("
    UPDATE {$wpdb->prefix}total_wallet_amount twa 
    SET total_wallet_amount = '%s' WHERE twa.user_email = %s;
    ", $new_total, $user_email));
}

// Add a discount (related to user wallet available amount)
add_action('woocommerce_cart_calculate_fees', 'adjust_order_total_with_deduction');
function adjust_order_total_with_deduction( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $current_user;
    
    if ($current_user->ID > 0) {
        $total_wallet_amount   = get_total_wallet_amount( $current_user->user_email );
        $cart_contents_total  = $cart->get_cart_contents_total(); 
        $cart_contents_total += $cart->get_cart_contents_tax(); 

        if ($total_wallet_amount > 0) {
            // calculations
            if( $total_wallet_amount >= $cart_contents_total ) {
                $deduction_amount = -$cart_contents_total;
            } else {
                $deduction_amount = -$total_wallet_amount;
            }
            $cart->add_fee(__('Deduction Amount', 'your-text-domain'), $deduction_amount);
        }
    }
}

// Update user wallet amount after order is placed (on processing or completed status)
add_action('woocommerce_order_status_processing', 'update_total_wallet_amount_from_order', 10, 2 );
add_action('woocommerce_order_status_completed', 'update_total_wallet_amount_from_order', 10, 2 );
function update_total_wallet_amount_from_order( $order_id, $order ){
    $wallet_updated = $order->get_meta('_user_wallet_updated'); // Check if user wallet has been updated yet

    if ( ! $wallet_updated ) {
        $current_user  = $order->get_user();
        $wallet_amount = (float) get_total_wallet_amount( $current_user->user_email ); // get user Wallet amount

        foreach( $order->get_items('fee') as $item_fee ) {
            if( $item_fee->get_name() === 'Deduction Amount' && $item_fee->get_total() < 0 ) {
                // Update wallet amount
                update_total_wallet_amount( $current_user->user_email, $wallet_amount + floatval($item_fee->get_total()) );
                // Tag order as "_user_wallet_updated" true
                $order->update_meta_data('_user_wallet_updated', '1'); 
                $order->save(); // save
                break;
            }
        }
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

wp_enqueue_scripts not loading(基于类的插件)

如何减少Laravel分页中的链接数

Laveel Livewire Get Groupby姓名列表

为什么在我的PHP联系人脚本中开机自检后出现未定义变量错误?

PHP-带POST验证的for循环

如何实现对数据库表中多列的唯一约束?

WooCommerce中基于用户角色的不同产品差价

如何将WooCommerce产品从一个产品复制到另一个产品S

Codeigniter 4中的未定义变量$HOME

如果WooCommerce购物车在 checkout 时被清空,请重定向至store 页面

在WooCommerce管理中为订单项目添加自定义字段价格值

根据类别的数量折扣更改WooCommerce购物车商品价格

Woocommerce 临时购物车税未根据第一个请求正确计算

PHP Laravel 迁移文件不创建关系

.htaccess 重写规则问题

正则表达式将文本替换为标签 html 以字符开头

Woocommerce单个添加到购物车页面中的产品动态定价

PHP/Laravel 中的自定义时区

按数字然后按字符串对关联 PHP 数组进行排序

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表