隐藏WooCommerce中特定送货类的送货方式

时间:2017-12-08 16:14:34

标签: php wordpress woocommerce cart shipping

基本上,当有购物商品的商品类别为“Roller”(ID Dup)时,我会尝试将统一费率方法ID ~Dup l -> NoDup l 停用

这是我尝试过的代码:

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

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:7)

更新2019年:您应该尝试这种更短,更紧凑,更有效的方式:

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;

    // HERE define your shipping class to find
    $class = 92;

    // HERE define the shipping method to hide
    $method_key_id = 'flat_rate:7';

    // Checking in cart items
    foreach( $package['contents'] as $item ){
        // If we find the shipping class
        if( $item['data']->get_shipping_class_id() == $class ){
            unset($rates[$method_key_id]); // Remove the targeted method
            break; // Stop the loop
        }
    }
    return $rates;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作。

  

有时,您可能需要刷新送货地区的送货方式,然后停用/保存并重新启用/保存您的“固定费率”送货方式。

相关: Hide shipping methods for specific shipping classes in WooCommerce

  

要查找送货方式ID和送货类ID,请参阅以下内容...

许多不同送货方式的更新(与您的评论相关):

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;

    // HERE define your shipping class to find
    $class = 92;

    // HERE define the shipping methods you want to hide
    $method_key_ids = array('flat_rate:7', 'local_pickup:3');

    // Checking in cart items
    foreach( $package['contents'] as $item ) {
        // If we find the shipping class
        if( $item['data']->get_shipping_class_id() == $class ){
            foreach( $method_key_ids as $method_key_id ){
                unset($rates[$method_key_id]); // Remove the targeted methods
            }
            break; // Stop the loop
        }
    }
    return $rates;
}

经过测试和工作......

查找货运类ID。

1)在wp_terms表格下的数据库中:

搜索术语名称或术语slug,您将获得术语ID(发货类ID)。

2)在使用浏览器html检查工具编辑“统一费率”的Woocommerce运输设置中,检查运费类费率字段,如:

enter image description here

在输入名称属性中,您有woocommerce_flat_rate_class_cost_64。所以64是发货类的ID。

获取送货方式费率ID:

  

要获取相关的送货方式费率ID ,例如 flat_rate:12 ,请使用浏览器代码检查器检查每个相关的单选按钮属性 {{ 1}} 喜欢:

     

enter image description here

答案 1 :(得分:1)

通过调整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地面”送货。

原始帖子中的场景(如果发货类别为 y ,则禁用方法 x )将像这样工作:

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