【问题标题】:Charge a conditional Delivery fee in WooCommerce 3在 WooCommerce 3 中收取有条件的送货费
【发布时间】:2018-03-04 19:10:22
【问题描述】:

这是我的情况:

如果小计

因此,如果小计符合此条件,那么我想收取运费,该费用也应显示在电子邮件发票中。

目前这是我的代码:

add_action( 'woocommerce_cart_calculate_fees','my_delivery_fee' );
function my_delivery_fee() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $subtotal = $woocommerce->cart->subtotal;
    if(subtotal <= certain amount && subtotal <= certain amount){
        $woocommerce->cart->add_fee( 'Delivery Fees', 5, true, 'standard' );
    } 

}

但它没有显示任何东西。

我做错了什么?

【问题讨论】:

    标签: php wordpress woocommerce cart shipping


    【解决方案1】:

    问题来自您的状况。应该是:

    如果小计小计>金额2

    另外你的代码有点老了,这样试试(累进费的例子):

    add_action( 'woocommerce_cart_calculate_fees','my_delivery_fee', 10, 1 );
    function my_delivery_fee( $wc_cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        $subtotal = $wc_cart->subtotal;
        $fee = 0;
    
        // Progressive fee
        if(subtotal < 25 ){ // less than 25 
            $fee = 5;
        } elseif( subtotal >= 25 && subtotal < 50){ // between 25 and 50
            $fee = 10;
        } else { // From 50 and up 
            $fee = 15;
        }
    
        if( fee > 0 )
            $wc_cart->add_fee( 'Delivery Fees', $fee, true, 'standard' );
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    所有代码都在 Woocommerce 3+ 上进行了测试并且可以运行。

    【讨论】:

    • Loic 我每次都会读到你的回答,你真的在​​做很棒的代码,你能指导我增加我的编码知识吗?
    猜你喜欢
    • 2019-09-08
    • 2021-02-24
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2018-02-10
    相关资源
    最近更新 更多