隐藏自定义运费当购物车中有多个物品时

huangapple go评论62阅读模式
英文:

Hide Custom Shipping When Multiple Items are in cart

问题

我添加了一个插件,当产品类别是"baby-care"且国家是"India"时,创建了一个运输方式,其ID为"pisol_extended_flat_shipping:52540",它正常运行。但当有人在购物车中添加来自不同类别的产品,其中之一是"baby-care"时,我希望在结账时隐藏"pisol_extended_flat_shipping:52540"运输方式,而有其他类别的产品。

我尝试了以下代码,但不起作用:

function check_cart_categories() {
    $baby_care = false;
    $other_categories = false;
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        if (has_term('baby-care', 'product_cat', $product->id)) {
            $baby_care = true;
        } else {
            $other_categories = true;
        }
    }
    return ($baby_care && $other_categories);
}

add_filter('woocommerce_available_shipping_methods', 'hide_pisol_extended_flat_shipping');
function hide_pisol_extended_flat_shipping($available_methods) {
    if (check_cart_categories()) {
        unset($available_methods['pisol_extended_flat_shipping:52540']);
    }
    return $available_methods;
}
英文:

I added a plugin that creates a shipping method with Id "pisol_extended_flat_shipping:52540" when the product category is "baby-care" and the country is "India" and It's working fine. When someone adds different products from different categories in the cart and one of the categories is "baby-care". then I want to hide the "pisol_extended_flat_shipping:52540" shipping when there are other category products at the checkout along with "baby-care".

I tried this code, but not working

function check_cart_categories() {
$baby_care = false;
$other_categories = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( has_term( 'baby-care', 'product_cat', $product->id ) ) {
$baby_care = true;
} else {
$other_categories = true;
}
}
return ( $baby_care && $other_categories );
} 
add_filter( 'woocommerce_available_shipping_methods', 'hide_pisol_extended_flat_shipping' );
function hide_pisol_extended_flat_shipping( $available_methods ) {
if ( check_cart_categories() ) {
unset( $available_methods['pisol_extended_flat_shipping:52540'] );
}
return $available_methods;
}

答案1

得分: 1

这是完整的代码:

function check_cart_categories() {
    // 在这里编写你的逻辑...
}

add_filter( 'woocommerce_package_rates', 'hide_pisol_extended_flat_shipping' , 10, 2 );

function hide_pisol_extended_flat_shipping( $rates, $package  ) {
    if ( check_cart_categories() ) {
        unset( $rates['pisol_extended_flat_shipping:52540'] );
    }
    return $rates;
}

如果它不起作用,尝试增加挂钩的优先级(我设置了默认的 `10`)。一些插件也可能使用这个挂钩并可能会干扰。
英文:

Here is the full code:

function check_cart_categories() {
    // your logic here...
}

add_filter( 'woocommerce_package_rates', 'hide_pisol_extended_flat_shipping' , 10, 2 );

function hide_pisol_extended_flat_shipping( $rates, $package  ) {
    if ( check_cart_categories() ) {
        unset( $rates['pisol_extended_flat_shipping:52540'] );
    }
    return $rates;
}

If it doesn't work, try increasing the priority of the hook (I set default 10). Some plugins are also using this hook and could interfere.

huangapple
  • 本文由 发表于 2023年2月7日 01:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定