在我的WooCommerce网站上,每种产品都有四个产品属性(Size、Color、Season和Brand).属性Size和Color用于变化,而Season和Brand不用于变化.
其目的是隐藏"S24"Season产品的免运费.我从以下代码开始,但它只适用于用于变体的属性:
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_season'; // 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(
'free_shipping:8' => array('s24'),
'flat_rate:17' => array('f23'),
);
// 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;
}
如何使其适用于不用于变体的产品属性?