【发布时间】:2021-03-29 09:24:28
【问题描述】:
我正在尝试根据我的购物车中的两个参数删除送货方式。 参数 1 = 产品 ID 参数 2 = 为该产品 ID 添加的数量。
我一直在搜索和组合不同的解决方案,但是使用下面的 sn-p 仍然不能给我正确的结果。预期结果是,如果任何产品(6、9、69、71)被添加到我的购物车 52 次,运费 (flexible_shipping_2_1) 应该会消失。
所有建议将不胜感激。
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
$product_ids = array( 6 , 9, 69 , 71 ); // HERE set the product IDs in the array
$method_id = 'flexible_shipping_2_1'; // HERE set the shipping method ID
$found = false;
// Loop through cart items Checking for defined product IDs
foreach( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) && $cart_item['quantity'] == 52){
$found = true;
break;
}
}
if ( $found )
unset( $rates[$method_id] );
return $rates;
}
【问题讨论】:
-
嗨 LoicTheAztec,感谢您在这件事上的支持和智慧,它真的帮助了我。
标签: php wordpress woocommerce shipping-method product-quantity