最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far