【发布时间】:2014-08-13 14:20:44
【问题描述】:
我正在尝试根据 Woocommerce (2.1.12) 中的类别禁用/隐藏运输方式。
我使用此功能成功实现了隐藏付款方式:
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');
但是,禁用送货方式似乎不起作用。 我尝试了三种不同的 sn-ps。
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 );
【问题讨论】:
标签: woocommerce options shipping