如何在 WooCommerce 中找到运输类别 ID?

Wil*_*rza 3 php wordpress woocommerce

在 woocommerce 答案代码中使用隐藏特定运输类别的运输方法,我试图根据运输类别隐藏运输选项。

大件物品和小件物品在我们的网站上有自己的运输类别,我们只想为小件物品提供快递运输。

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;
}
Run Code Online (Sandbox Code Playgroud)

我了解如何实现代码,但是我不知道在哪里可以找到运输类别 id,我只能找到 slug 和运输类别名称

// HERE define your shipping class to find

$class = 92;
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我在哪里可以找到替换92 的Id ?

提前致谢。

将要

Loi*_*tec 6

为了使其与运输类别“Slugs”一起使用,您将使用WC_Product get_shipping_class()以下方法:

\n\n
add_filter( \'woocommerce_package_rates\', \'hide_shipping_method_based_on_shipping_class\', 10, 2 );\nfunction hide_shipping_method_based_on_shipping_class( $rates, $package )\n{\n    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )\n        return;\n\n    // HERE define your shipping class SLUG\n    $class_slug = \'large\';\n\n    // HERE define the shipping method to hide\n    $method_key_id = \'flat_rate:7\';\n\n    // Checking in cart items\n    foreach( WC()->cart->get_cart() as $cart_item ){\n        // If we find the shipping class\n        if( $cart_item[\'data\']->get_shipping_class() == $class_slug ){\n            unset($rates[$method_key_id]); // Remove the targeted method\n            break; // Stop the loop\n        }\n    }\n    return $rates;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(或活动主题)的 function.php 文件中...

\n\n
\n

有时,您可能需要刷新前往送货区域的送货方式,然后禁用/保存并重新启用/保存您的“统一费率”送货方式。

\n\n

要查找运输方式 ID 和运输类别 ID,请参阅下面\xe2\x80\xa6

\n
\n\n
\n\n

查找运输类别 ID。

\n\n

1)数据库中表下wp_terms:

\n\n

搜索术语名称或术语别名,您将获得术语 ID(运输类别 ID)。

\n\n

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

\n\n

在此输入图像描述

\n\n

在输入名称属性中,您有woocommerce_flat_rate_class_cost_64. 因此 64 是运输类别的 ID。

\n\n
\n\n

获取送货方式费率 ID:

\n\n
\n

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

\n\n

在此输入图像描述

\n
\n