如果购物车中存在特定的产品类别,请禁用基于权重的WooCommerce送货方式

时间:2017-11-30 04:05:21

标签: wordpress woocommerce shipping

如果将具有特定产品类别的产品添加到购物车中,如何禁用特定的基于重量的送货方式?

该类别为冷冻食品,禁用方法为标准运费(1-3天)

我已经尝试过这段代码,但它对我不起作用:

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(
        '150'
     );
    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['standard_delivery_charge'] ); // Replace "local_pickup" with the shipping option that you want to remove.
    }
    // return the available methods without the one you unset.
    return $available_methods;
}

0 个答案:

没有答案