Created
November 4, 2019 21:37
-
-
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
This file contains 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_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