Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

php - 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 :

enter image description here

Can this be done through editing the PHP?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

This code is tested and works.


2) Using a custom function hooked in woocommerce_cart_item_name filter hook, this way:

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

Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...