6

本质上,当购物车商品的运输类别为“Roller”(ID )时,我试图flat_rate:7 禁用统一费率方法 Id。92

这是我试过的代码:

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
    $hide_when_shipping_class_exist = array(
        92 => array(
            'flat_rate:7'
        )
    );

    $shipping_class_in_cart = array();
    foreach(WC()->cart->cart_contents as $key => $values) {
       $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
    }

    foreach($hide_when_shipping_class_exist as $class_id => $methods) {
        if(in_array($class_id, $shipping_class_in_cart)){
            foreach($methods as & $current_method) {
                unset($available_shipping_methods[$current_method]);
            }
        }
    }

    return $available_shipping_methods;
}

运输类别 ID92是运输类别,我想隐藏flat_rate:7它。

我的网站是这样的:http: //www.minimoto.me/ WordPress:4.8.4 WooCommerce:3.1.1

任何帮助将不胜感激。

4

2 回答 2

16
于 2017-12-09T20:13:20.603 回答
3

通过调整 LoicTheAztec 的代码(欢呼),我能够根据其内容的运输类别,而不是整个购物车来取消设置每个包裹的运输方式。也许它也会对其他人有所帮助:

// UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

        $product_id = $package_item['product_id']; // Grab product_id
        $_product   = wc_get_product( $product_id ); // Get product info using that id

        if( $_product->get_shipping_class_id() != 371 ){ // If we DON'T find this shipping class ID
            unset($rates['wbs:9:dae98e94_free_ups_ground']); // Then remove this shipping method
            break; // Stop the loop, since we've already removed the shipping method from this package
        }
    }
    return $rates;
}

如果包裹包含除“标准”物品以外的任何物品(在我的情况下为shipping_class_id 371),此代码允许我取消设置“免费 UPS Ground”运输。

原始帖子中的场景(禁用方法x如果运输类y)将像这样工作:

// UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

        $product_id = $package_item['product_id']; // Grab product_id
        $_product   = wc_get_product( $product_id ); // Get product info using that id

        if( $_product->get_shipping_class_id() == 92 ){ // If we DO find this shipping class ID
            unset($rates['flat_rate:7']); // Then remove this shipping method
            break; // Stop the loop, since we've already removed the shipping method from this package
        }
    }
    return $rates;
}
于 2018-07-12T13:47:30.087 回答