【问题标题】:Woocommerce Hide Shipping method based on categoryWoocommerce 隐藏基于类别的运输方法
【发布时间】: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


    【解决方案1】:

    我有一个类似的请求,并将您的解决方案与我发现的其他一些解决方案相结合,并得到了您需要的工作。

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

    以下是从可用运输方式中取消设置“local_pickup”的工作功能:

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

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 1970-01-01
      • 2019-10-17
      • 2021-02-04
      • 2018-07-10
      • 2021-09-19
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多