我正在尝试根据Woocommerce(2.1.12)中的类别来解除/隐藏送货方式。
我成功地使用此功能隐藏了paymeny方法:
function filter_gateways($gateways){
$category_ID = array('14'); // <----------- add the ids for which u want to turn off "cash on delivery"
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $category_ID ) ) {
unset($gateways['cod']);
break;
}
break;
}
}
return $gateways;
}
add_filter('woocommerce_available_payment_gateways','filter_gateways');
但是,禁用送货方式似乎不起作用。 我尝试了三种不同的片段。
No.1(不工作):
function custom_shipping_methods( $available_methods ) {
$category_ID = array('14'); // <----------- add the ids for which u want to turn off "local pickup"
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $category_ID ) ) {
unset( $available_methods['local_pickup'] );
break;
}
break;
}
}
return $available_methods;
}
add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' );
No.2(不工作):
function custom_shipping_methods( $is_available ) {
$category_ID = array('14'); // <----------- add the ids for which u want to turn off "local pickup"
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $category_ID ) ) {
$is_available = false;
break;
}
break;
}
}
return $is_available;
}
add_filter( 'woocommerce_shipping_local_pickup_is_available', 'custom_shipping_methods' );
No.3(不工作)我试过这个,因为显然更新版的woocommerce使用下面的过滤器:
function hide_local_pickup( $rates, $package ) {
$category_ID = array('14'); // <----------- add the ids for which u want to turn off "local pickup"
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $category_ID ) ) {
unset( $rates['local_pickup'] );
break;
}
break;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_local_pickup' , 10, 2 );
答案 0 :(得分:0)
我有类似的请求,并将您的解决方案与我找到的其他一些解决方案相结合,并得到了您需要的工作。
显然,woocommerce并没有在购物车阵列中包含标签或类别,因此您必须先获取它,然后再执行您的功能。
下面是一个工作函数,用于取消设置&#39; local_pickup&#39;从可用的运输方式:
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// specify the category id's you want to hide local_pickup
$category_ID = array(
'6', // books
'7', // prints
'13', // candles
'8', // note cards
'9', // origonal art
);
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the categories. Switch to true if the category is found.
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $category_ID ) ) {
$found = true;
break;
}
}
}
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['local_pickup'] ); // Replace "local_pickup" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}