【问题标题】:Percentage discount on shipping based on order total in WooCommerce基于 WooCommerce 中订单总额的运费折扣百分比
【发布时间】:2020-06-26 11:20:18
【问题描述】:

我正在尝试通过插件或functions.php 找到一种方法,如果订单在25 到50 英镑之间,如何仅将运费折扣50%。

我正在使用“Set a percentage discount to Local pickup shipping method in Woocommerce ”,它根据运输方式打折,但我希望它对所有运输方式都打折,因为我们有很多而且它没有最低订购量的规则。

【问题讨论】:

    标签: php wordpress woocommerce shipping discount


    【解决方案1】:
    • 您可以使用woocommerce_package_rates 挂钩

    • 如果订单在 25 英镑到 50 英镑之间,这将为运费提供 50% 的折扣。

    function filter_woocommerce_package_rates( $rates, $package ) {
        /* Settings */
        $min = 25;
        $max = 50;
        $discount_percent = 50;
    
        // Get cart total
        $cart_total = WC()->cart->cart_contents_total;
    
        // Condition
        if ( $cart_total >= $min && $cart_total <= $max ) {
            // (Multiple)
            foreach ( $rates as $rate_key => $rate ) {
                // Get rate cost            
                $cost = $rates[$rate_key]->cost;
                
                // Set rate cost
                $rates[$rate_key]->cost = $cost - ( ( $cost * $discount_percent ) / 100 );
            }
        }
    
        return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
    

    【讨论】:

    • 完美!谢谢老哥!
    • 需要清除我的购物车缓存,是的,它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多