Hi @msdesignfoto,
There is a plugin available for WooCommerece called Conditional Shipping and Payments which allows the restriction shipping methods and payment gateways using conditional logic. This would allow you to restrict what shipping methods display based on what shipping classes were in the customer’s cart.
With the default Flate Rate shipping method in WooCommerce, you can configure that with advanced costs and calculate costs per item in cart. This may work for your purpose of increasing the cost based on a product quantity.
https://docs.woocommerce.com/document/flat-rate-shipping/#advanced-costs
Hi @msdesignfoto,
For removing a shipping method when a product in a certain class is added, you could consider using plugins such as Conditional Shipping and Payments, Conditional Shipping for WooCommerce, etc.
As for varying the shipping costs based on the quantity of items in cart, have you tried using the Flat Rate Shipping – Advanced costs?
Hello and thank you for your input.
I have managed to make the php snippet work, I had some trouble finding the shipping method ID but now that part is done (hiding a shipping method when a certain class is on cart).
This is the code I used to hide the cheap shipping method when any product of class 2 or 3 are in the cart:
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 SLUG
$class_slug = 'classe-2'; 'classe-3';
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:2';
// 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() == $class_slug ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
Now I only need to set different costs for certain amount of a certain product. But this should be done for 2 specific products, not the total amount in the cart. They are only 2 so I can create specific shipping classes for these 2 products to make it easier to filter them for coding purposes.
Glad to hear that you were able to make the code work!
Thanks for sharing the snippet as well.
I’ll mark this thread as resolved now. If you have any further questions, I recommend creating a new thread.
Hello!
In fact I haven’t managed to do that after all, that code I posted earlier was not working. As some user said in another post, I should be using an array. Even with the array, it was not working (I must have used the array incorrectly) so I ended up with an alternative: 3 similar snippets. The only change is the filter name. Rest of the code is the same. Altough I would prefer if this could be achieved in a single snippet, but for now is working.
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class_caixas', 10, 2 );
function hide_shipping_method_based_on_shipping_class_caixas( $rates, $package )
{
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class_kits', 10, 2 );
function hide_shipping_method_based_on_shipping_class_kits( $rates, $package )
{
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class_trajes', 10, 2 );
function hide_shipping_method_based_on_shipping_class_trajes( $rates, $package )
{
Cheers
-
This reply was modified 5 years, 4 months ago by
msdesignfoto.
-
This reply was modified 5 years, 4 months ago by
msdesignfoto.