在我的WooCommerce网站上,每种产品都有四个属性(尺寸、 colored颜色 、季节和品牌).Size和Colour属性用于变化,而Season和Brand不用于变化.
我设置了3种送货方式:免费送货(ID 17),没有最低订单,免费送货(ID 8),最低订单100欧元,最后支付送货(ID 1)9.99欧元.
我希望在从S24季节购买产品时有最低订单100欧元的免费送货(ID 17)和9.99欧元的付费送货(ID 1),而如果您从F23季节购买产品,则必须激活最低订单100欧元和付费送货(ID 1)9.99欧元的免费送货(ID 8).在最后一种情况下,如果您购买了至少一个S24季节的产品和至少一个F23季节的产品,则必须只激活没有最低订单的免费送货(ID 17).
以下是开始代码,但它只适用于用于变体的属性:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 10, 2 );
function hide_shipping_method_based_on_product_attribute( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Product Attribute taxonomy (start always with "pa_")
$taxonomy = 'pa_season'; // Example for "Season"
// Array of Shipping method rate ID and term(s) slug(s) pairs
$data_array = array(
'free_shipping:8' => array('s24'),
'free_shipping:17' => array('f23'),
'flat_rate:1' => array('f23'),
);
// Loop through items in the current shipping package
foreach( $package['contents'] as $item ){
// Get the parent variable product if it's a product variation
$product = $item['variation_id'] > 0 ? wc_get_product($item['product_id']) : $item['data'];
// Loop through our data array
foreach( $data_array as $rate_id => $term_slugs ) {
// loop through product attributes
foreach($product->get_attributes() as $attribute ) {
if( $attribute->get_taxonomy() === $taxonomy && isset($rates[$rate_id])
&& count( array_intersect($attribute->get_slugs(), $term_slugs) ) > 0 ) {
unset($rates[$rate_id]);
}
}
}
}
return $rates;
}