【问题标题】:Applied coupons disable Free shipping conditionally in Woocommerce在 Woocommerce 中应用的优惠券有条件地禁用免费送货
【发布时间】:2021-02-24 15:21:43
【问题描述】:

我正在尝试获取它,以便如果客户添加优惠券代码(其中任何一个),免费送货选项将消失,并且将实施统一费率费用。 - 你会认为这将是一件容易实现的事情,会有 100 种插件和描述的方法来做到这一点,但我还没有找到。我不想为插件支付 89 美元来做这件事

如果他们使用优惠券但消费超过 249 美元,他们仍然有资格享受免费送货服务。我读了一些如何做到这一点的地方,但它需要我获取 POST ID,而最新的 WooCommerce 无法像以前那样使用它,我不知道运输 ID,所以我很迷茫这是代码

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
$free_shipping_id = 'free_shipping:2';
unset($packages[0]['rates'][ $free_shipping_id ]);
}
return $packages;
} );

谢谢

编辑

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 
10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return $rates;

$min_total      = 250; // Minimal subtotal allowing free shipping

// Get needed cart totals
$total_excl_tax = WC()->cart->get_total();
$discount_excl_tax = WC()->cart->get_discount_total();


// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $total_excl_tax - $discount_excl_tax;

$applied_coupons   = WC()->cart->get_applied_coupons();

if( sizeof($applied_coupons) > 0 &&  $discounted_subtotal_incl_taxes < $min_total ) {
    foreach ( $rates as $rate_key => $rate ){
        // Targeting "Free shipping"
        if( 'free_shipping' === $rate->method_id  ){
            unset($rates[$rate_key]);
        }
    }
}
return $rates;

}

【问题讨论】:

    标签: php wordpress woocommerce coupon shipping-method


    【解决方案1】:

    默认情况下,如果优惠券代码已填写并在购物车中处于活动状态,WooCommerce 仍将允许选择免费送货方式。如果使用优惠券代码,可以将以下代码添加到您孩子的 functions.php 页面以挂钩并隐藏任何免费送货选项。

    add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $applied_coupons = WC()->session->get( 'applied_coupons', array() );
    if ( ! empty( $applied_coupons ) ) {
    $free_shipping_id = 'free_shipping:2';
    unset($packages[0]['rates'][ $free_shipping_id ]);
    }
    return $packages;
    } );
    

    只需将上述代码中的 $free_shipping_id 更改为您的免费送货选项的 ID。您可以通过转到 WooCommerce > 设置 > 运输选项找到您的 ID,然后单击您的免费送货选项。帖子 ID 将位于显示免费送货详细信息/设置的页面的 URL 中。

    i.e www.yourdomain.com/wp-admin/post.php?post=40107&action=edit
    

    在此示例中,其中 40107 是发货 ID。您的送货 ID 会有所不同。

    【讨论】:

    • 这是我获得上述代码的地方,当我进入 Woo > Settings > Shipping Options 时,我没有看到任何 Post ID.. 这就是我来这里的原因
    【解决方案2】:

    只有当购物车小计达到最低金额(含税折扣)时,以下代码才会为应用的优惠券启用“免费送货”:

    add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
    function coupons_removes_free_shipping( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        $min_subtotal      = 250; // Minimal subtotal allowing free shipping
        
        // Get needed cart subtotals
        $subtotal_excl_tax = WC()->cart->get_subtotal();
        $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
        $discount_excl_tax = WC()->cart->get_discount_total();
        $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
        
        // Calculating the discounted subtotal including taxes
        $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
        
        $applied_coupons   = WC()->cart->get_applied_coupons();
    
        if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
            foreach ( $rates as $rate_key => $rate ){
                // Targeting "Free shipping"
                if( 'free_shipping' === $rate->method_id  ){
                    unset($rates[$rate_key]);
                }
            }
        }
        return $rates;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。


    原答案:

    以下代码将在无需任何设置的情况下应用任何优惠券时删除“免费送货”送货方式。您的实际代码中有一些错误。请尝试以下操作:

    add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
    function coupons_removes_free_shipping( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        $applied_coupons = WC()->cart->get_applied_coupons();
    
        if( sizeof($applied_coupons) > 0 ){
            // Loop through shipping rates
            foreach ( $rates as $rate_key => $rate ){
                // Targeting "Free shipping" only
                if( 'free_shipping' === $rate->method_id  ){
                    unset($rates[$rate_key]); // Removing current method
                }
            }
        }
        return $rates;
    }
    

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

    【讨论】:

    • 太棒了,非常感谢.. 您是否也知道如何制作它,以便如果应用了优惠券但折扣后总计超过 249 可以免费送货。 - 我仍在学习 woocommerce 的所有钩子
    • @KangOnRails 我已经更新了我的答案,其中将以最小的小计金额启用免费送货......
    • 这就是我喜欢编程社区的原因。非常感谢您的帮助。
    • 这样工作完美无缺,但对于小计,我需要总计至少为 250.. 我尝试这样做(在原始帖子中编辑) - 但它不起作用。,..跨度>
    • @KangOnRails 你不能对总计进行计算……只能对小计进行计算
    最近更新 更多