php - WooCommerce - Add Shipping class below each product in Shopping Cart page

admin2025-06-07  57

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 :

I'm new in customising wordpress. Any guidance on this would be much appreciated.

Thanks!

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 :

I'm new in customising wordpress. Any guidance on this would be much appreciated.

Thanks!

Share Improve this question asked Jan 20, 2018 at 9:16 scholarwithfirescholarwithfire 1132 bronze badges 2
  • how did you configure shipping class ? custom field ? – melvin Commented Jan 20, 2018 at 10:53
  • WooCommerce settings > Shipping > Shipping Class – scholarwithfire Commented Jan 20, 2018 at 13:07
Add a comment  | 

1 Answer 1

Reset to default 0

You can try 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>';
}

Add this to your functions.php file of the active theme. Tested on Woocommerce 3.5.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749247612a317592.html

最新回复(0)