中隐藏特定运输类别的运输方式

中隐藏特定运输类别的运输方式

本文介绍了在 WooCommerce 中隐藏特定运输类别的运输方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,当购物车商品的运输类别为Roller"(ID 92).

这是我试过的代码:

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);函数 wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package){$hide_when_shipping_class_exist = 数组(92 =>大批('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) {如果(in_array($class_id,$shipping_class_in_cart)){foreach($methods as & $current_method) {未设置($available_shipping_methods[$current_method]);}}}返回 $available_shipping_methods;}

运输等级 ID 92 是运输等级,我想为它隐藏 flat_rate:7.

我的网站是这样的:

在输入名称属性中,您有 woocommerce_flat_rate_class_cost_64.所以 64 是运输类的 ID.


获取运输方式费率 ID:

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

Essentially I'm trying to make the flat rate method Id flat_rate:7 disabled when there is cart items that have the shipping class "Roller" (ID 92).

This is the code I tried:

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

Shipping class ID 92 is the shipping class and I want to hide flat_rate:7 for it.

My Site is this: http://www.minimoto.me/WordPress: 4.8.4WooCommerce: 3.1.1

Any help will be greatly appreciated.

解决方案

Update 2019: You should try instead this shorter, compact and effective way:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

Related: Hide shipping methods for specific shipping classes in WooCommerce


Update for many different shipping methods (related to your comments):

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

Tested and works…


Finding the shipping class ID.

  1. In the database under wp_terms table:

Search for a term name or a term slug and you will get the term ID (the shipping class ID).

  1. On Woocommerce shipping settings editing a "Flat rate", with your browser html inspector tool, inspect a shipping Class rate field like:

In the imput name attribute you have woocommerce_flat_rate_class_cost_64. So 64 is the ID for the shipping class.


Get the shipping method rate ID:

这篇关于在 WooCommerce 中隐藏特定运输类别的运输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:04