简体   繁体   English

在WooCommerce Shopping Cart页面的每个产品下面添加Shipping类

[英]Add Shipping class below each product in WooCommerce Shopping Cart page

I've configure shipping class for my products. 我为我的产品配置了运输类。 But I want to display them below each product in the shopping cart page. 但我想在购物车页面中的每个产品下面显示它们。 Something like this : 像这样的东西:

在此输入图像描述

Can this be done through editing the PHP? 这可以通过编辑PHP来完成吗?

To display the product shipping class name in cart page, there is many ways to do it: 要在购物车页面中显示产品运输类名称,有很多方法可以执行此操作:

1) Using a custom function hooked in woocommerce_cart_item_name filter hook, this way: 1)使用挂钩在woocommerce_cart_item_name过滤器钩子中的自定义函数,这样:

add_filter( 'woocommerce_get_item_data', 'shipping_class_in_item_name', 20, 2 );
function shipping_class_in_item_name( $cart_data, $cart_item ) {

    $custom_items = array();

    $product = $cart_item['data']; // Get the WC_Product object instance
    $shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
    $shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
    $label = __( 'Shipping class', 'woocommerce' );

    // Checking (To display it in checkout page too, remove below " || is_checkout()" )
    if( empty( $shipping_class_id ) || is_checkout() )
        return $cart_data; // Return default cart dat (in case of)

    // If product or variation description exists we display it
    $custom_items[] = array(
        'key'      => $label,
        'display'  => $shipping_class_term->name,
    );

    // Merging shipping class name and product variation attributes + values (if there are some)
    if( ! empty( $cart_data ) ) $custom_items = array_merge( $custom_items, $cart_data );

    return $custom_items;
}

Code goes in function.php file of the active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。

This code is tested and works. 此代码经过测试和运行。


2) Using a custom function hooked in woocommerce_cart_item_name filter hook, this way: 2)使用挂在woocommerce_cart_item_name过滤器挂钩中的自定义函数,这样:

add_filter( 'woocommerce_cart_item_name', 'shipping_class_in_item_name', 20, 3);
function shipping_class_in_item_name( $item_name, $cart_item, $cart_item_key ) {
    // Only in cart page (remove the line below to allow the display in checkout too)
    if( ! ( is_cart() || is_checkout() ) ) return $item_name;

    $product = $cart_item['data']; // Get the WC_Product object instance
    $shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
    $shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );

    if( empty( $shipping_class_id ) )
        return $item_name; // Return default product title (in case of)

    $label = __( 'Shipping class', 'woocommerce' );

    return $item_name . '<br>
        <p class="item-shipping_class" style="margin:12px 0 0;">
            <strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
}

Code goes in function.php file of the active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。

Tested and works. 经过测试和工作。

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

相关问题 在 Woocommerce 单个产品的“添加到购物车”下方显示特定的运输类别 - Display specific shipping class below "Add to cart" on Woocommerce single products Woocommerce:如果当前产品在购物车中,则将主体 class 添加到产品页面 - Woocommerce : Add Body class to product page if the current product is in the cart 如果产品具有特定的运输类别,则向 WooCommerce 购物车项目添加自定义文本 - Add a custom text to WooCommerce cart items if the product has a specific shipping class Woocommerce将购物车风格添加为产品页面 - Woocommerce Add to cart style as product page woocommerce 添加到单个产品页面上的购物车按钮 - woocommerce add to cart button on single product page Ajax 加入购物车 WooCommerce 产品页面 - Ajax add to cart on WooCommerce product page 在Woocommerce的“结帐”页面上将链接添加到购物车 - Add link to Shopping Cart on Woocommerce's Checkout page 如果产品在购物车中,则在woocommerce中添加类以添加到购物车按钮 - if product is in cart add class to add to cart button in woocommerce 如何在购物车页面“WooCommerce”中显示运输等级的零费率值 - How to show zero rate value of shipping class in cart page “WooCommerce” 在 Woocommerce 中的产品摘要下方添加额外的添加到购物车按钮 - Adding Extra Add to cart button below product summary in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM