On this put up, I’m going to indicate you learn how to cover Woocommerce delivery strategies for particular delivery courses. This lets you cover all wanted delivery strategies through the use of a Woocommerce delivery class.
Earlier than you bounce in a fast remark although: all of the snippets proven beneath will go to both your little one theme’s features.php file or, higher but, use the Code Snippets plugin for it (like I do).
If you happen to’re a newbie, then I’d counsel you check out this video right here beneath as a result of in it I’ll present you precisely learn how to accomplish all that.
Tips on how to Disguise Woocommerce delivery for particular delivery courses?
Now, there are two belongings you would want to do beforehand:
- Add a delivery class. Go to Woocommerce >> Settings >> Transport >> Transport courses and add one. For instance: “Courier solely”
- Write down the delivery class slug as a result of you’ll need it later. The place to get the slug? See the screenshot beneath.
- Add delivery class to the wanted product. Open your product and see below the Transport tab.
If that is out of the best way let’s add the primary snippet. So, go to Snippets >> Add new and paste this snippet inside your code field. Listen although that it’s worthwhile to change the delivery class slug on line 4 accordingly.
So, if on this instance I’ve a “Courier solely” delivery class with a slug “courier-only”, then substitute the slug within the code accordingly with the proper one.
Another factor, on line 12 there’s a delivery technique that you’re hiding (Flat fee). Change the delivery technique identify and ID as wanted. Tips on how to discover your delivery ID quantity? Properly, check out the video above (see the 3min 44sec mark).
/* Disguise Woocommerce delivery technique for a particular delivery class */
operate hide_flat_rate_shipping( $charges, $bundle ) {
// delivery class IDs that want the tactic eliminated
$shipping_classes = array('courier-only'); // Right here goes your delivery class slug
$if_exists = false;
foreach( $bundle['contents'] as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
$if_exists = true;
}
if( $if_exists ) unset( $charges['flat_rate:2'] ); // Right here goes your delivery technique that must be hidden. Don’t neglect so as to add right one right here
return $charges;
}
add_filter( 'woocommerce_package_rates', 'hide_flat_rate_shipping', 10, 2 );
There’s additionally an alternate strategy to cover a number of delivery strategies for numerous delivery courses utilizing a single operate. For instance, on this case:
- If a product belongs to the delivery class ‘package1,’ we’ll deactivate the flat fee for zones 1, 2, and three.
- If the product belongs to the delivery class ‘package2,’ we’ll deactivate native pickup for zones 1, 2, and three.
operate wpsh_hide_shipping( $charges, $bundle ) {
// Outline an array that maps delivery courses to the delivery strategies that have to be hidden
$class_to_methods = array(
'package1' => array(
'flat_rate:4', // Flat fee for Zone 1
'flat_rate:5', // Flat fee for Zone 2
'flat_rate:6' // Flat fee for Zone 3
),
'package2' => array(
'local_pickup:3', // Native pickup for Zone 1
'local_pickup:4', // Native pickup for Zone 2
'local_pickup:5' // Native pickup for Zone 3
)
);
$shipping_class = false;
foreach( $bundle['contents'] as $key => $values ) {
$shipping_class = $values['data']->get_shipping_class();
if( array_key_exists($shipping_class, $class_to_methods) ) {
foreach ($class_to_methods[$shipping_class] as $technique) {
unset($charges[$method]);
}
}
}
return $charges;
}
add_filter( 'woocommerce_package_rates', 'wpsh_hide_shipping', 10, 2 )
Tips on how to Disguise A number of Woocommerce delivery strategies for particular delivery courses?
Within the earlier instance, we hid a Flat fee technique. However what if it’s worthwhile to cover a number of Woocommerce delivery strategies for particular delivery courses? Properly, simply take the code beneath and see the feedback contained in the code.
See the instance that I’m utilizing to cover three totally different delivery strategies (SmartPost, DPD, and Flat fee) for the Courier-only delivery class.
/* Disguise Woocommerce delivery technique for a particular delivery class (without cost delivery) */
operate wpsh_hide_shipping( $charges, $bundle ) {
// Add your delivery class IDs that want the tactic eliminated
$shipping_classes = array('cargobus');
$if_exists = false;
foreach( $bundle['contents'] as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
$if_exists = true;
}
// Add your delivery strategies that want the tactic eliminated
if( $if_exists ) {
unset( $charges['local_pickup:2'] ); // Native pickup
unset( $charges['flat_rate:1'] ); // Flat fee
}
return $charges;
}
add_filter( 'woocommerce_package_rates', 'wpsh_hide_shipping', 10, 2 );
Tips on how to show the Woocommerce delivery class identify on the cart and checkout pages?
Subsequent, I’m going to show the Woocommerce delivery class identify on the cart and checkout pages. It can seem beneath the product title. Therefore, let’s use this piece of code.
// Show Woocommerce delivery class on cart web page
add_filter( 'woocommerce_cart_item_name', 'display_shipping_class_in_cart_and_checkout', 20, 3);
operate display_shipping_class_in_cart_and_checkout( $item_name, $cart_item, $cart_item_key )
Tips on how to show a delivery class-based customized message on Woocommerce cart and checkout pages?
First, why would it’s worthwhile to show a delivery class-based customized message on the Woocommerce cart and checkout pages? I exploit it as a result of if I cover delivery strategies my buyer could also be confused about why there’s solely a Courier and no different strategies obtainable.
Due to this fact, with this snippet right here beneath, I’ll show this practice message for the merchandise which have a “Courier solely” delivery class. “A few of your merchandise have a delivery class “Courier solely”. For that cause we will supply solely this delivery technique.”
/* Show a hipping class primarily based customized message on Woocommerce cart and checkout pages */
add_action( 'woocommerce_review_order_before_order_total', 'display_shipping_class_message' );
add_action( 'woocommerce_cart_totals_after_order_total', 'display_shipping_class_message' );
operate display_shipping_class_message(){
$shipping_classes = array('courier-only'); // Right here goes your delivery class slugs within the array
// Right here goues your message proven within the cart and checkout web page
$message = __('<robust>A few of your merchandise have a delivery class "Native pickup solely". For that cause we will supply solely native pickup as a delivery technique.</robust>', "woocommerce");
// Loop via cart objects
foreach( WC()->cart->get_cart() as $cart_item ){
$shipping_class = $cart_item['data']->get_shipping_class();
// echo '<pre>' . print_r($shipping_class, true) . '</pre>'; // Uncomment for testing
// Verify cart objects for particular delivery class, displaying a message
if( in_array($shipping_class, $shipping_classes ) ){
// Change NB! textual content if wanted
echo '<tr class="shipping-note">
<td colspan="2"><robust>'.__("NB!", "woocommerce").'</robust> '.$message.'</td>
</tr>';
break;
}
}
}
Now, in the event you add all these snippets as I confirmed you, then this ought to be the consequence. As you see from the screenshot beneath:
- One product has a “Transport: Courier solely” class
- The one obtainable delivery technique is Courier and all different ones (Free delivery included) are hidden
- There’s a customized message proven beneath the delivery totals