微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Woocommerce 中选择某些产品变体时隐藏特定的运输方式

如何解决在 Woocommerce 中选择某些产品变体时隐藏特定的运输方式

在要求避免重复问题之前,我已经进行了研究。如果有的话,我深表歉意!
我找到的最接近的是@LoicTheAztec 的这段代码,我要感谢他的贡献!但我同时涉及两个属性,所以我不知道如何正确实现。

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

    // HERE define the Product Attibute taxonomy (starts always with "pa_")
    $taxonomy = 'pa_color'; // Example for "Color"

    // HERE define shipping method rate ID to be removed from product attribute term(s) slug(s) (pairs) in this array
    $data_array = array(
        'flat_rate:12'      => array('blue'),'local_pickup:13'   => array('black','white'),);

    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( isset($cart_item['variation']['attribute_'.$taxonomy]) ) {
            // The product attribute selected term slug
            $term_slug = $cart_item['variation']['attribute_'.$taxonomy];

            // Loop through our data array
            foreach( $data_array as $rate_id => $term_slugs ) {
                if( in_array($term_slug,$term_slugs) && isset($rates[$rate_id]) ) {
                    // We remove the shipping method corresponding to product attribute term as defined
                    unset($rates[$rate_id]);
                }
            }
        }
    }
    return $rates;
} 

我的案例场景

添加了一种特定的送货方式:flat_rate:7,它仅对一个特定的送货区域可见。

我的可变产品由三个主要属性组成:样式 - 颜色 - 尺寸

我想在选择这些变体时隐藏此运输方式flat_rate:7

款式 = 连帽衫
尺寸 = 4XL & 5XL

PS:我不使用任何运输类。
我希望我的描述清楚

感谢任何帮助。 谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。