• Resolved msdesignfoto

    (@msdesignfoto)


    Hello

    I need a a way to hide 1 shipping method when the client’s cart has at least 1 item of a certain class. We have several cheap and light products for Shipping Class 1, but whenever the cart has a product of Class 2 or 3, I would like to disable the cheapest shipping method. I read around and found 2 php functions but none worked. Seems they were not generic solutions or I wasn’t able to use them.

    Is there a way to do this by code or plugin?

    We also need to provide different shipping rates according to the amount of products the client buys, for a few specific produtcts, but I believe this should be done separately.

    Thank you

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    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

    Plugin Support abwaita a11n

    (@abwaita)

    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?

    Thread Starter msdesignfoto

    (@msdesignfoto)

    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.

    Plugin Support abwaita a11n

    (@abwaita)

    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.

    Thread Starter msdesignfoto

    (@msdesignfoto)

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide Shipping method for Shipping Class’ is closed to new replies.