Woocommerce 基于类别的配送方法

Woocommerce 基于类别的配送方法,woocommerce,options,shipping,Woocommerce,Options,Shipping,我正在尝试根据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; f

我正在尝试根据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');
功能过滤器\网关($gateways){
$category_ID=array('14');//购物车->购物车内容为$key=>$value){
$terms=获取术语($values['product\u id','product\u cat');
foreach($terms作为$term){
if(在数组中($term->term\u id,$category\u id)){
未设置($gateways['cod']);
打破
}
打破
}
}
返回$gateways;
}
添加过滤(“woocommerce可用的支付网关”、“过滤网关”);
但是,禁用装运方法似乎不起作用。 我试了三个不同的片段

1号(不工作):

函数自定义装运方法($available\u methods){
$category_ID=array('14');//购物车->购物车内容为$key=>$value){
$terms=获取术语($values['product\u id','product\u cat');
foreach($terms作为$term){
if(在数组中($term->term\u id,$category\u id)){
未设置($available_methods['local_pickup']);
打破
}
打破
}
}
返回$available\u方法;
}
添加过滤器(“woocommerce可用的装运方法”、“自定义装运方法”);
第2号(不工作):

函数自定义装运方法($is_可用){
$category_ID=array('14');//购物车->购物车内容为$key=>$value){
$terms=获取术语($values['product\u id','product\u cat');
foreach($terms作为$term){
if(在数组中($term->term\u id,$category\u id)){
$is_available=false;
打破
}
打破
}
}
return$可用;
}
添加过滤器(“woocommerce\u shipping\u local\u Pick\u可用”,“custom\u shipping\u方法”);
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 );
函数隐藏本地拾取($rates,$package){
$category_ID=array('14');//购物车->购物车内容为$key=>$value){
$terms=获取术语($values['product\u id','product\u cat');
foreach($terms作为$term){
if(在数组中($term->term\u id,$category\u id)){
未结算($rates['local_-pickup']);
打破
}
打破
}
}
返回美元汇率;
}
添加过滤器('woocommerce\u package\u rates'、'hide\u local\u Pick',10,2);

我有一个类似的请求,并将您的解决方案与我找到的其他解决方案结合起来,得到了您需要的解决方案

显然,woocommerce在购物车数组中不包含标签或类别,因此您必须先获取标签或类别,然后执行您的功能

以下是一个工作功能,用于从可用的装运方式中取消“本地提货”:

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;

}
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 );
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;

}