【问题标题】:Hide free shipping when specific coupons are applied in Woocommerce在 Woocommerce 中应用特定优惠券时隐藏免费送货
【发布时间】:2020-12-03 11:23:15
【问题描述】:

我正在尝试设置它,以便特定优惠券不提供免费送货服务。

然后我尝试了这个,但它不起作用

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $applied_coupons = WC()->session->get( 'applied_coupons', array() );
    if ( ! empty( $applied_coupons ) ) {
        
        if (in_array("544", $applied_coupons))
        {
        
            $free_shipping_id_11 = 'free_shipping:11';
            if($free_shipping_id_11){
                unset($packages[0]['rates'][ $free_shipping_id_11 ]);
            }
            $free_shipping_id_9 = 'free_shipping:9';
            if($free_shipping_id_9){
                unset($packages[0]['rates'][ $free_shipping_id_9 ]);
            }
        
        }
        
    }
    return $packages;
} );

【问题讨论】:

    标签: php wordpress woocommerce cart shipping-method


    【解决方案1】:

    请尝试以下方法:

    add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
    function filter_woocommerce_package_rates( $rates, $package ) {
        $targeted_coupons = array("544"); // Here set your related coupon codes
    
        $applied_coupons = WC()->cart->get_applied_coupons();
    
        if ( ! empty($applied_coupons) && array_intersect( $targeted_coupons, $applied_coupons ) ) {
            foreach ( $rates as $rate_key => $rate ) {
                if ( 'free_shipping' === $rate->method_id ) {
                    unset($rates[$rate_key]);
                }
            }
        }
        return $rates;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    刷新运输缓存:

    1. 此代码已保存在您的functions.php 文件中。
    2. 在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。

      您已完成,您可以对其进行测试。

    【讨论】:

    • 不幸的是,这似乎不起作用? rolypoly.co.za
    • @user3037553 使用优惠券“summer”试试on this test web site(Storefront 主题的最后一个 Woocommerce 版本)。如您所见,它有效。因此,您的网站上还有其他问题。它可以是您添加的自定义代码,您的主题进行的一些自定义或错误的设置。由于此答案有效并回答了您的问题,您可以请accept 回答,谢谢。
    猜你喜欢
    • 2021-10-28
    • 2021-01-05
    • 2014-03-03
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多