Created
August 5, 2019 13:48
-
-
Save Garconis/e99b79955dd49b042dc8d5e0284ad110 to your computer and use it in GitHub Desktop.
How to display the Shipping Class of each item in the WooCommerce shopping cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add the shipping class to the bottom of each item in the cart | |
*/ | |
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 ) { | |
// If the page is NOT the Shopping Cart or the Checkout, then return the product title (otherwise continue...) | |
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' ); | |
// Return default product title (in case of no Shipping Class) | |
if( empty( $shipping_class_id ) ) { | |
return $item_name; | |
} | |
// If the Shipping Class slug is either of these, then add a prefix and suffix to the output | |
if ( ( $shipping_class_term->slug == 'flat-1995-per' ) || ( $shipping_class_term->slug == 'flat-4999-per' ) ) { | |
$prefix = '$'; | |
$suffix = 'each'; | |
} | |
$label = __( 'Shipping Class', 'woocommerce' ); | |
// Output the Product Title and the new code which wraps the Shipping Class name | |
return $item_name . '<br> | |
<p class="item-shipping_class" style="margin:0.25em 0 0; font-size: 0.875em;"> | |
<em>' .$label . ': </em>' . $prefix . $shipping_class_term->name . ' ' . $suffix . '</p>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment