0

在要求避免重复问题之前,我已经完成了研究。如果有的话,我道歉!。
我找到的最接近的是@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 :

款式=连帽衫
尺码= 4XL5XL

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

任何帮助表示赞赏。谢谢!

4

0 回答 0