0
我想要删除或添加一些运输选项,具体取决于购物车中的产品类别。例如:我有一个来自X类的产品,我想将shipping-method-X添加到购物车。如果我有一个来自X类的产品,另一个来自另一个类,则shipping-method-X将被禁用。 下面的函数检查数组是否包含ID = 49的类别,并且如果有另一个类别,但它不起作用。什么也没有发生在车:(请帮Woocommerce基于类别添加或隐藏运输方法
add_filter('woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1);
function check_cart_for_share() {
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
foreach ($woocommerce->cart->cart_contents as $key => $values) {
$terms = get_the_terms($values['product_id'], 'product_cat');
$tmp = array();
foreach ($terms as $term) {
array_push($tmp, $term->term_id);
}
array_unique($tmp);
if (sizeof($tmp) > 1 AND in_array('49', $tmp)) {
$found = true;
}
}
return $found;
}
function hide_shipping_based_on_tag($available_methods) {
// use the function above to check the cart for the categories.
if (check_cart_for_share()) {
// remove the method you want
unset($available_methods['flat_rate']);
}
// return the available methods without the one you unset.
return $available_methods;
}