如何在 WooCommerce 中找到运输类别 ID? [英] How do I find the shipping class id in WooCommerce?

查看:21
本文介绍了如何在 WooCommerce 中找到运输类别 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

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

<小时>

获取送货方式费率 ID:

<块引用>

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

Using Hide shipping method for specific shipping classes in woocommerce answer code, I'm trying to hide shipping options based on shipping class.

Large items and small items have their own shipping class on our website and we only want to offer express shipping on small items.

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( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class_id() == $class ){
            unset($rates[$method_key_id]); // Remove the targeted method
            break; // Stop the loop
        }
    }
    return $rates;
}

I understand how to implement the code, however I don't know where to find the shipping class id, I can only find the slug and shipping class name

// HERE define your shipping class to find

$class = 92;

in the above example, where do I find the Id to replace the 92?

Thanks In advance.

Will

解决方案

To make it work with shipping class "Slugs" you will use WC_Product get_shipping_class() method instead this 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 SLUG
    $class_slug = 'large';

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

    // Checking in cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class() == $class_slug ){
            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 active theme)...

Sometimes, you should may be need to refresh shipping methods going to shipping areas, then disable / save and re-enable / save your "flat rates" shipping methods.

To find the shipping methods IDs and the shipping classes IDs see below…


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).

2) 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:

To get the related shipping methods rate IDs, something like flat_rate:12, inspect with your browser code inspector each related radio button attribute name like:

这篇关于如何在 WooCommerce 中找到运输类别 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆