我试图添加两个费用,所有WooCommerce订单时,如果购物车有"拍卖"产品在它.

我需要为购物车中的每个拍卖产品增加20的俱乐部费用,并 for each 拍卖产品额外的15%的佣金.

我的问题是:我不知道如何计算这些费用,因为佣金是根据每种产品的中标价格计算的.一张可以是100美元,另一张是50美元--以此类推.

这就是我试过的,当在一个本地主机上测试two个拍卖产品时,似乎不起作用,每个拍卖产品价值$100美元.总费用应该是$15 + $15 + $20 + $20 = $70美元,但我在 checkout 页面上什么也没有得到.完全不收费.

This is the code:

add_action( 'woocommerce_cart_calculate_fees', 'commission_and_fees', 10, 1 );
function commission_and_fees( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
if ( ! is_user_logged_in() ) return;

    // define arguments
    $clubbing_fee = '';
    $percentage_fee_per_item = '';
    
    // false, init
    $is_auction = false;

    // loop through and break if any auction products are found
    foreach ( $cart_object->cart_contents as $item ) {
        
        // when found, break
        if ( $item['data']->is_type( 'auction' ) ) {
            $is_auction = true;
            break;
        }
    }
 
    if ( $is_auction ) {
        
        $percentage_fee_per_item = 15;
        $clubbing_fee = 20;
        $percentage_and_clubbing_fee = $cart_object->cart_contents_total * $percentage_fee_per_item / 100 + $clubbing_fee;
        
        $cart_object->add_fee( "Auction Fees ($percentage_fee_per_item% + Clubbing Fee)", $percentage_and_clubbing_fee, true );
    }
}

推荐答案

您的代码…中有许多错误例如,$cart_object没有定义.

以下将是

  • 对拍卖物品加收15%的费用
  • 每件拍卖品加收20美元费用

代码:

add_action( 'woocommerce_cart_calculate_fees', 'auctions_commission_and_fees' );
function auctions_commission_and_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() ) 
        return;

    $clubbing_fee    = 20; // Fixed (per auction item)
    $commission_rate = 0.15; // Percentage rate: 15%
    $items_qty_count = 0; // Initializing
    $items_subtotal  = 0; // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        if ( $item['data']->is_type('auction') ) {
            $items_qty_count += $item['quantity']; // Quantity count
            $items_subtotal  += $item['line_subtotal']; // Subtotal
        }
    }
 
    if ( $items_qty_count > 0 ) {
        // Commission fee (15% on auctions subtotal)
        $cart->add_fee( 
            __('Commission Fee (15%)', 'woocommerce'), 
            $items_subtotal * $commission_rate, 
            true 
        );

        // Clubbing fee ($20 per auction item)
        $cart->add_fee( 
            __('Clubbing Fee ($20 per item)', 'woocommerce'), 
            $clubbing_fee * $items_qty_count, 
            true 
        );
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.

Php相关问答推荐

PHP在将数组子集赋给变量时是否复制数组子集?

在WooCommerce中执行一次银行电汇确认付款代码

如何发送woocommerce订单跟踪码与短信

按制造商、型号和年份范围判断数据的存在性

如何使用php-amqplib连接到ActiveMQ Classic Docker镜像

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

针对给定产品类别的WooCommerce计数审核

如何将LARAVEL模块路径和项目路径合并,因为当我使用模块路径时,它们会覆盖项目路径

Regex:高级删除单行PHP注释

在WooCommerce循环中添加子类别和产品之间的分隔符

如何在自定义邮箱内容中获取WooCommerce订单项目的详细信息?

从 WooCommerce 购物车中删除总计部分,同时保留小计行

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

安装 Herd 并返回 Valet 后为 502

在PHP WordPress插件中使用变量连接路径时的问题解决方法

将Shopware 6后端中定义的值导入MultiFilter

PHP使用三元运算符进行null合并(再次)

服务器遇到内部错误或配置错误,无法完成您的请求

自定义错误页面不适用于 Apache 和 nginx 设置的 .htaccess