Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/865694c018a19d919355788a8121cc9a to your computer and use it in GitHub Desktop.
Save Garconis/865694c018a19d919355788a8121cc9a to your computer and use it in GitHub Desktop.
WooCommerce | Display the shipping class of items in the cart, and change shipping methods (and verbiage) based on the shipping classes in the cart
<?php
/**
* Hide a Flat Rate option (instance #7) when the "Call for Quote" shipping class is in the cart
* from: https://www.bolderelements.net/support/knowledgebase/hide-shipping-method-when-cart-contains-shipping-class/
*/
function fs_hide_shipping_when_class_is_in_cart( $rates, $package ) {
// shipping class IDs (slugs) that need the method removed
$shipping_classes = array('call-for-quote');
$if_exists = false;
foreach( $package['contents'] as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
$if_exists = true;
}
if( $if_exists ) unset( $rates['flat_rate:4'] );
return $rates;
}
add_filter( 'woocommerce_package_rates', 'fs_hide_shipping_when_class_is_in_cart', 10, 2 );
/**
* Change the verbiage if/when there is "No Shipping methods available"
* from: https://aceplugins.com/doc/advanced-shipping-for-woocommerce/how-to-change-the-no-shipping-methods-available-message/
*/
add_filter( 'woocommerce_no_shipping_available_html', 'fs_custom_no_shipping_message' );
add_filter( 'woocommerce_cart_no_shipping_available_html', 'fs_custom_no_shipping_message' );
function fs_custom_no_shipping_message( $message ) {
return __( 'Please call 800-634-0344 to request a quote. Otherwise, remove the item from the cart that isn\'t shippable.' );
}
/**
* Add the shipping class to the bottom of each item in the cart
* from: https://wordpress.stackexchange.com/questions/291637/woocommerce-add-shipping-class-below-each-product-in-shopping-cart-page
*/
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)
if ( ( $shipping_class_term->slug == 'flat-1995-per' ) || ( $shipping_class_term->slug == 'flat-4999-per' ) ) {
$prefix = '$';
$suffix = 'each';
}
$label = __( 'Shipping', 'woocommerce' );
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