简体   繁体   English

更改 WooCommerce 发货方式 全 label 根据产品发货 class

[英]Change WooCommerce shipping method full label based on product shipping class

I want to add a custom message on the cart page in WooCommerce. The message should appear depending on the shipping class assigned to the product, in the woocommerce_cart_shipping_method_full_label hook.我想在 WooCommerce 的购物车页面上添加自定义消息。该消息应根据分配给产品的运费 class 在woocommerce_cart_shipping_method_full_label挂钩中显示。

I already have code that does it, but it doesn't work when I assign it to that hook, it only works if I assign it to the woocommerce_before_calculate_totals hook.我已经有了执行此操作的代码,但是当我将它分配给该挂钩时它不起作用,只有当我将它分配给woocommerce_before_calculate_totals挂钩时它才起作用。

When I want to add it to the woocommerce_cart_shipping_method_full_label hook, I get the message:当我想将它添加到woocommerce_cart_shipping_method_full_label挂钩时,我收到消息:

Fatal error: Uncaught Error: Call to a member function get_cart()致命错误:未捕获错误:调用成员 function get_cart()

Could someone advise me on what I'm doing wrong?有人可以告诉我我做错了什么吗? I am using the storefront template with a child theme.我正在使用带有子主题的店面模板。

Based on Cart Message for a Specific Shipping Class in WooCommerce answer code, this is the code I'm using in the functions.php:基于WooCommerce 答案代码中特定运输 Class 的购物车消息,这是我在函数中使用的代码。php:

add_action( 'woocommerce_cart_shipping_method_full_label', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
    if ( ! is_cart() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
        return;

    if ( did_action( 'woocommerce_cart_shipping_method_full_label' ) >= 2 )
        return;

    $shipping_class_id = '28'; // Your shipping class Id

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        // Check cart items for specific shipping class, displaying a notice
        if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
            wc_clear_notices();

            wc_add_notice( sprintf( __('My custom message.', 'woocommerce'), '' . __("Pallet Shipping", "woocommerce") . ''), 'notice' );

            break;
        }
    }
}

Mapping code written for one hook to another hook sometimes takes some tweaking, depending on the original hook and the newly mapped hook:将为一个钩子编写的代码映射到另一个钩子有时需要进行一些调整,具体取决于原始钩子和新映射的钩子:

  • For example, $cart is not passed to the callback function, and hence the error message you get例如, $cart未传递给回调 function,因此您收到错误消息
  • wc_clear_notices() and wc_add_notice() are not applicable, as the hook you wish to use is to change the $label wc_clear_notices()wc_add_notice()不适用,因为您希望使用的挂钩是更改$label
  • if ( did_action( 'woocommerce_cart_shipping_method_full_label' ) >= 2 ) does not exist if ( did_action( 'woocommerce_cart_shipping_method_full_label' ) >= 2 )不存在

So you get:所以你得到:

function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
    // Your shipping class Id
    $shipping_class_id = 28;

    // WC Cart
    if ( WC()->cart ) {
        // Get cart
        $cart = WC()->cart;

        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Check cart items for specific shipping class
            if ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) {
                $label = __( 'My custom message', 'woocommerce' ); 
                break;
            }
        }
    }

    return $label; 
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce 根据选择的运输类别在结账时更改运输方式标题 - Woocommerce Change shipping method title on checkout based on shipping class selected 根据 Woocommerce 3 中的运输类别过滤运输方法 - Filter Shipping method based on shipping class in Woocommerce 3 Woocommerce,隐藏基于运输类的运输方式 - Woocommerce, hide shipping method based on shipping class 根据 Woocommerce 中购物车中的运费 class 价格项目更改运费 class - Change shipping class based on shipping class price items in cart in Woocommerce 根据 Woocommerce 中运送 class 计数的购物车项目更改运送 class - Change shipping class based on cart items shipping class count in Woocommerce Woocommerce:根据购物车商品的运输类别计数更改运输类别 - Woocommerce: Change shipping class based on cart items shipping class count 根据单选按钮更改 Woocommerce 发货方式 - change Woocommerce shipping method based on radio buttons 根据运输方式更改 Woocommerce 订单状态 - Change Woocommerce Order Status based on Shipping Method WooCommerce:根据购物车中的数量更改运输类别 - WooCommerce: Change shipping class based on quantity in cart 自动设置 WooCommerce 产品出货 class 基于产品类别 - Auto set WooCommerce product shipping class based on product categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM