【问题标题】:How do I find the shipping class id in WooCommerce?如何在 WooCommerce 中找到运输类别 ID?
【发布时间】:2018-12-13 20:37:07
【问题描述】:

使用Hide shipping method for specific shipping classes in 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;
}

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

// HERE define your shipping class to find

$class = 92;

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

提前致谢。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    要使其与运输类 "Slugs" 一起使用,您将使用 WC_Product get_shipping_class() 方法,而不是这样:

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

    代码进入您的活动子主题(或活动主题)的 function.php 文件...

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

    要查找运输方式 ID 和运输类别 ID,请参见下文……


    查找运输类别 ID。

    1)wp_terms表下的数据库中:

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

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

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


    获取运输方式费率 ID:

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

    【讨论】:

    • 以上内容非常有用,如果其他人试图使用这个 sn-p: // 这里定义运输方法以隐藏 $method_key_id = 'flat_rate:7';要查找运输方法 ID,最简单的方法是使用您要排除的方法转到 WooCommerce 设置 > 运输 > 运输区域 > 右键单击​​要排除的方法并选择在新选项卡中打开。然后查看 url 的末尾并使用该 id。例如 url 可能是:yoursite.com/wp-admin/… 要隐藏的运输方式的 id 是 18
    • 非常棒的技巧如何在 DOM 中找到 slug!
    猜你喜欢
    • 1970-01-01
    • 2019-10-17
    • 2021-02-04
    • 1970-01-01
    • 2014-07-05
    • 2016-03-08
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多