php - 应用优惠券有条件地在 Woocommerce 中禁用免费送货

标签 php wordpress woocommerce coupon shipping-method

我正在尝试获取它,以便如果客户添加优惠券代码(其中任何一个),则免费送货选项将消失,并且将实现统一费率费用。 - 你可能会认为这很容易实现,有数百个插件和方法可以实现这一点,但我还没有找到。我不想为做这一件事的插件支付 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;

}

最佳答案

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

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_excl_tax + 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 文件中。经过测试并有效。

关于php - 应用优惠券有条件地在 Woocommerce 中禁用免费送货,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52912181/

相关文章:

javascript 下拉停止在 Wordpress Woocommerce https 页面上工作

php - WordPress wp_remote_post

php - 如何在数据库写入和读取时防止并发?

php - CSS3 列垂直跨越

css - 组织具有许多断点和许多模板的 CSS 文件

javascript - 实现 AJAX post 的问题

css - 为什么我的段落元素大小不同?

php - 将 get 参数从 javascript 传递给 php 会破坏格式

woocommerce - 为什么订单完成后我不能添加项目元?

wordpress - WooCommerce 产品网格(重新排列产品详细信息)