// check for shipping class in cart
function cart_has_product_with_shipping_class($slug)
{
global $woocommerce;
$product_in_cart = false;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
$terms = get_the_terms($_product->id, 'product_shipping_class');
if ($terms) {
foreach ($terms as $term) {
$_shippingclass = $term->slug;
if ($slug === $_shippingclass) {
$product_in_cart = true;
}
}
}
}
return $product_in_cart;
}
// remove all but desired for selected_category products
add_filter('woocommerce_package_rates', 'hide_all_but_desired_if_selected_category', 99);
function hide_all_but_desired_if_selected_category($rates)
{
$categories = array('selected-category-slug');
//if selected_category is in the cart
if (woo_custom_category_is_in_the_cart($categories)) {
unset($rates['table_rate:24:2']); // zone 1
unset($rates['table_rate:22']); // zone 2
}
return $rates;
}
1条答案
按热度按时间ryevplcw1#
我从@businessbloomer那里学到了这个!第一部分是一个函数,你可以把它留在函数中。第二部分检查购物车中的特定类别,你可以根据那只猫是否在购物车中取消设置运输方法
您可以通过检查您的购物车运输方法获得这些$率