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/56ab97956b24699f580df07f5db44d80 to your computer and use it in GitHub Desktop.
Save Garconis/56ab97956b24699f580df07f5db44d80 to your computer and use it in GitHub Desktop.
WooCommerce | Adjust the title of the Shipping Method based if a Shipping Class product is in the Cart
<?php
add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
function change_shipping_method_name_based_on_shipping_class($rates, $package){
// HERE set the shipping class for "Pickup"
$shipping_class_id = 125;
$pickup = false;
$deliver = false;
// Check for the "Pickup" shipping class in cart items
foreach( $package['contents'] as $cart_item ) {
// if a product has Pickup class, then set this variable to true
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$pickup = true;
//break;
}
// if a product does NOT have pickup class, then set this variable to true
if( $cart_item['data']->get_shipping_class_id() !== $shipping_class_id ){
$deliver = true;
break;
}
}
// Loop through shipping methods
foreach ( $rates as $rate_key => $rate ) {
// Change "Flat rate" Shipping method label name
if ( 'flat_rate' === $rate->method_id ) {
// if it had a deliver item then make no mention of pickup
if($deliver) {
$rates[$rate_key]->label = __( 'Delivery Rate', 'woocommerce' );
}
// otherwise it must have had ONLY pickup items
elseif($pickup) {
$rates[$rate_key]->label = __( 'Local Pickup', 'woocommerce' );
}
// just in case neither of those ends up being true
else {
$rates[$rate_key]->label = __( 'Shipping', 'woocommerce' );
}
}
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment