The products in my website are handled by one of that 2 shipping plugins: Printful Integration for WooCommerce and Printify for WooCommerce Shipping. when there is mixed items from each shipping plugin. Those plugins split each one the shipping package in two when there is mixed items (which is a a conflict and a problem).
So I have added a shipping class 'printful' (which id is 548) to the products that are handled by the Printful plugin, and tried to adjust Hide shipping method for specific shipping classes in woocommerce answer code by #LoicTheAzec (cheers), to only remove the shipping method from a specific duplicated shipping packages with ids 2 and 3 due to the conflict between the shipping plugins…
Here is my actual code:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 548; //CAMDEN HARBOR CHART MUG is in shipping class
// HERE define the shipping methods you want to hide
$method_key_ids = array('printify_shipping_s', 'printify_shipping_e');
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
But it's not working and I still get 4 shipping packages instead of two:
Any help is appreciated.
The problem here is related to splitting packages conflict between your two shipping plugins, when mixed items are in cart. In that case each plugin split the shipping package, which add 4 split packages instead of 2.
Those plugins are using woocommerce_cart_shipping_packages to split the shipping packages with an unknown priority (so I will set a very high priority).
The following code will keep the first 2 split packages from cart (and checkout too):
add_filter( 'woocommerce_cart_shipping_packages', 'remove_split_packages_based_on_items_shipping_class', 100000, 1 );
function remove_split_packages_based_on_items_shipping_class( $packages ) {
$has_printful = $has_printify = false; // Initializing
// Lopp through cart items
foreach( WC()->cart->get_cart() as $item ){
// Check items for shipping class "printful"
if( $item['data']->get_shipping_class() === 'printful' ){
$has_printful = true;
} else {
$has_printify = true;
}
}
// When cart items are mixed (using both shipping plugins)
if( $has_printful && $has_printify ){
// Loop through split shipping packages
foreach( $packages as $key => $package ) {
// Keeping only the 2 first split shipping packages
if( $key >= 2 ){
// Removing other split shipping packages
unset($packages[$key]);
}
}
}
return $packages;
}
Code goes in function.php file of your active child theme (active theme). It should works and display only two shipping packages when cart items are mixed.
Related
Here's the scenario:
I ship trays of drinks using UPS. However, it becomes very difficult to pack 5 trays of drinks into boxes. So I would like to disable UPS shipping method and only display Flat Rate Shipping if a customer orders 5 or more trays of drinks. I have about 7 different drinks, but I can add these drinks to a shipping class to simplify the code.
I want to expand this code to include the quantity of products in a specific the class or perhaps the number of times a product in the shipping class appears. So if the cart has 5 or more of a product in this specific shipping class, it should remove the shipping methods I've specified under array.
How do I expand this code to also include quantity of products?
// If we find the shipping class & quantity of product in the shipping class is equal to or greater than 5.
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping Class To Find
$class = 182;
// Number Of Shipping Class Items In Cart
$amount = 5;
// Shipping Methods To Hide
$method_key_ids = array('wf_shipping_ups:07', 'wf_shipping_ups:08', 'wf_shipping_ups:11', 'wf_shipping_ups:54', 'wf_shipping_ups:65', 'wf_shipping_ups:70', 'wf_shipping_ups:74', 'free_shipping:2', 'request_shipping_quote');
// Checking In Cart Items
foreach( $package['contents'] as $item ) {
// If We Find The Shipping Class and Number of Items
if( $item['data']->get_shipping_class_id() == $class && count($package['contents']) >= $amount ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove Targeted Methods
}
break; // Stop The Loop
}
}
return $rates;
}
Edit - Addition:
Since I purchased the "WooCommerce UPS Shipping Plugin with Print Label" from PluginHive, I have access to their "Manage Shipping Methods" plugin which allows me to do the following:
Set multiple rules to exclude various shipping methods from various shipping classes.
Break the sequence on first occurrence.
The rules I have set-up are as follows:
For Class 150 (Break on First Occurrence) - Unset:
wf_shipping_ups:07,
wf_shipping_ups:08,
wf_shipping_ups:11,
wf_shipping_ups:54,
wf_shipping_ups:65,
wf_shipping_ups:70,
wf_shipping_ups:74,
free_shipping:2,
request_shipping_quote.
For Class 151 - Unset:
flat_rate:20,
flat_rate:21.
I've created a third class 182 in the above code for the products I want to target. It should be treated as class 151 only if less than 5 items of the class are added to the cart.
But it should be treated as Class 150 if 5 or more items are added to the cart.
That's my dilemma.
Potential Solution - Addition:
I figured out how to solve my problem. The code #LoicTheAztec assisted me with lets me unset shipping methods for a given shipping class if the product quantity in the cart is 5 or more.
What I need to do now is unset two other shipping methods (flat_rate:20 and flat_rate:21) which is causing the conflict, for the same shipping class (182) but this time for a product quantity in the cart of 4 or less (=<).
Then I can use the existing plugin to create the following rules:
Break on First Occurrence (Check)
For Class 150 - Unset:
wf_shipping_ups:07,
wf_shipping_ups:08,
wf_shipping_ups:11,
wf_shipping_ups:54,
wf_shipping_ups:65,
wf_shipping_ups:70,
wf_shipping_ups:74,
free_shipping:2,
request_shipping_quote.
For Class 182 - Unset:
Nothing - Because Both Codes Will Create The Logic
For Class 151 - Unset:
flat_rate:20,
flat_rate:21.
This should resolve the conflict caused by the plugin.
The million dollar question is...can I somehow use #LoicTheAztec 's solution to set some sort of minimum quantity?
The following will hide specific defined shipping methods if total items from specific shipping class are 5 or more:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package ) {
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids = array( // Shipping Method rates Ids To Hide
'wf_shipping_ups:07',
'wf_shipping_ups:08',
'wf_shipping_ups:11',
'wf_shipping_ups:54',
'wf_shipping_ups:65',
'wf_shipping_ups:70',
'wf_shipping_ups:74',
'free_shipping:2',
'request_shipping_quote'
);
$related_total_qty = 0;
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(), $targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods
foreach( $shipping_rates_ids as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Untested it should works.
Refresh the shipping caches:
This code is already saved on your functions.php file.
In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
Handling number of items instead of items cumulated quantity:
Replace:
$related_total_qty += $cart_item['quantity'];
by
$related_total_qty++;
I'm setting up a website with shipping but i have items that are collection on for all shipping zone and item that can be sent in all shipping zone.
So I have set up shipping classes for all the zones.
I am using "Hide shipping method for specific shipping classes in woocommerce" answer code and it is what I need.
But instead of putting in each flat_rate id is there a way I can target all the Flat Rate shipping methods, so when I add an other flat rate shipping setting, it will work for it, without having me making changes into the code.
I hope you understand what I am after. Any help is appreciated.
To use it for all "flat rate" Shipping methods and some other defined shipping methods, you will use the following instead:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 92;
// HERE define the shipping methods you want to hide (others than "flat rate")
$method_key_ids = array('local_pickup:3');
$found = false;
// Checking in cart items
foreach( $package['contents'] as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
$found = true;
break; // Stop the loop
}
}
if( ! $found )
return $rates;
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// Targetting "Flat rate" and other defined shipping mehods
if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches: (required)
This code is already saved on your active theme's function.php file.
The cart is empty
In a shipping zone settings, disable / save any shipping method, then enable back / save.
I am using Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 lightly changed answer code to hide all shipping methods except one. The only method I want showing is a rate from the "Woocommerce Advanced Shipping" plugin.
I am using the correct rate ID etc...
Everything works fine except when a customer tries to click that shipping method, it won't stay selected. It just jumps back to free shipping.
I have tried debugging and also tried the code with a native woocommerce flat rate ID and it showed up/able to select it just fine.
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
$flat_rates_express = array( '2588' );
$free = $flat2 = array();
foreach ( $rates as $rate_key => $rate ) {
// Updated Here To
if ( in_array( $rate->id, $flat_rates_express ) )
$flat2[ $rate_key ] = $rate;
if ( 'free_shipping:12' === $rate->id )
$free[ $rate_key ] = $rate;
}
return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}
ID I want to Keep Shown: "2588" (Custom Shipping Rate From Plugin)
How can I disable the Flat rate shipping method when free shipping is available o and keep a custom shipping rate (from a plugin)?
As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:
add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
// Here your free shipping rate Id
$free_shipping_rate_id = 'free_shipping:12';
// When your Free shipping method is available
if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ) {
// Removing "Flat rate" shipping method
if ( 'flat_rate' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
This code is already saved on your function.php file.
In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
On Woocommerce, we need to remove the shipping-methods from the Cart section an add it to the Checkout page only.
Any track or help should be really appreciated?
There will be multiple ways to do it depending on the "why?" and on "for what?" you need this:
1) Hide shipping related from cart - The easiest way;
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_on_cart' );
add_filter( 'woocommerce_cart_needs_shipping', 'disable_shipping_on_cart' );
function disable_shipping_on_cart( $enabled ){
return is_checkout() ? true : false;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
But it will not remove the shipping methods (or shipping packages) from session…
2) Remove all shipping methods (and shipping packages) everywhere except in checkout page:
// Shipping methods
add_filter( 'woocommerce_package_rates', 'keep_shipping_methods_on_checkout', 100, 2 );
function keep_shipping_methods_on_checkout( $rates, $package ) {
if ( ! is_checkout() ) {
// Loop through shipping methods rates
foreach( $rates as $rate_key => $rate ){
unset($rates[$rate_key]); // Remove
}
}
return $rates;
}
// Shipping packages
add_filter( 'woocommerce_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
add_filter( 'woocommerce_cart_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
function keep_shipping_packages_on_checkout( $packages ) {
if ( ! is_checkout() ) {
foreach( $packages as $key => $package ) {
WC()->session->__unset('shipping_for_package_'.$key); // Remove
unset($packages[$key]); // Remove
}
}
return $packages;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
It will remove all shipping methods and all shipping packages from cart and WC_Session.
The related registered WC_Session data will be something like:
WC_Session_Handler Object
(
[_data:protected] => Array
(
[previous_shipping_methods] => a:1:{i:0;a:3:{i:0;s:16:"free_shipping:10";i:1;s:12:"flat_rate:14";i:2;s:15:"local_pickup:13";}}
[shipping_method_counts] => a:1:{i:0;i:3;}
[chosen_shipping_methods] => a:1:{i:0;s:16:"free_shipping:10";}
)
)
without shipping package…
It will only keep the previous shipping methods and the previous chosen shipping method for customers that have already purchased something before.
I need help in woocommerce shipping options, I want to hide flat rate for a particular product category, where I only want to show local delivery or local pickup options.
For all others categories all options should work.
I have try to do it with that code (added in function.php file of my theme):
function cart_has_product_with_orange_cats() {
global $woocommerce;
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
if($terms){
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ( $_categoryid == 16 ) {
//category is in cart!
$product_in_cart = true;
}
}
}
}
return $product_in_cart;
}
// add filter and function to hide method
add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' , 10, 1 );
function custom_shipping_methods( $available_methods ){
if ( cart_has_product_with_orange_cats() ) {
foreach($available_methods as $key => $method){
if( $key == 'local_delivery' || $key == 'local_pickup'){
continue;
}
unset($available_methods[$key]);
}
// remove the rate you want
}
// return the available methods without the one you unset.
return $available_methods;
}
But it isn't working for me, maybe because outdated code for latest WooCommerce version or any other issue.
How can I achieve this?
Update (Compatible with WC 3+ and works with variable products too)
The fully functional tested and corrected code for this answer is located on another thread:
Shipping methods - Local Pickup option not available when Flat Rate is hidden
Important: woocommerce_available_shipping_methods hook is deprecated since WC version 2.1+ and you will need to use instead woocommerce_package_rates filter hook.
WooCommerce version 2.6+ introduce NEW shipping zones. So all WooCommerce prior version's code related to shipping rates is outdated and will not work anymore.
For info: global $woocommerce;** with $woocommerce->cart has been replaced by WC()->cart.
You will use instead WC()->cart->get_cart()…
We will not need anymore your custom conditional function, because there is already a conditional function that exist in WordPress, that you can target for WooCommerce product categories. This function accept the term ID, the term slug or the term name:
has_term( 'your_category', 'product_cat', $post_id )
So we can use has_term() conditional function in this code:
add_filter( 'woocommerce_package_rates', 'conditional_hide_shipping_methods', 100, 2 );
function conditional_hide_shipping_methods( $rates, $package ){
// Define/replace here your correct category slug (!)
$product_category = 'your_category';
$prod_cat = false;
// Going through each item in cart to see if there is anyone of your category
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( has_term( $product_category, 'product_cat', $product_id ) ){
$prod_cat = true;
}
}
$rates_arr = array();
if ( $prod_cat ) {
foreach($rates as $key => $rate) {
if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) {
$rates_arr[ $rate_id ] = $rate;
break;
}
}
}
return !empty( $rates_arr ) ? $rates_arr : $rates;
}
Before adding the snippet, make sure you clear your WooCommerce cache (WooCommerce > System Status > Tools > WC Transients > Clear transients), as shipping methods are cached.
This code goes on function.php file of your active child theme or theme.
This code should work if you have correctly set your shipping zones…
References:
Hide other shipping methods when FREE SHIPPING is available
WooCommerce - Hide other shipping methods when FREE SHIPPING is available
WooCommerce 2.6 - Hiding paid shipping when free shipping is triggered by reaching specific amount
WooCommerce Cart - Conditional Items categories validation
How to Setup Shipping Zones in WooCommerce 2.6