Search code examples
phpwordpresswoocommercewoocommerce-subscriptions

WooCommerce - Different shipping methods for recurring orders


I am developing a site with the following plugins:

  • WooCommerce
  • WooCommerce Subscriptions
  • Pakkelabels.dk for WooCommerce

"Pakkelabels.dk" is a packaging label plugin for carriers in Denmark. This plugin is using the standard WooCommerce filters and hooks to add additional shipping methods.

I am using a mixed checkout. The cart totals currently looks like this:

enter image description here

This is what I wan't to do

For recurring orders I wan't to limit the shipping methods to just "DAO Pakkeshop" and "Local pick up" (sorry for the Danish language in the image).

I have added this to functions.php, which unsets the shipping methods I don't wan't to have, when a specific product ID (the subscription product) is in the cart:

add_filter( 'woocommerce_package_rates', 'hide_shipping_methods_woo_sg', 10, 2 );
function hide_shipping_methods_woo_sg( $rates, $package ) {

    $product_id = get_field('product_auto_cart', 'option');

    if($product_id){
        $product_cart_id = WC()->cart->generate_cart_id( $product_id );
        $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

        if($in_cart) {
            unset( $rates['pakkelabels_shipping_dao_direct'] );
            unset( $rates['pakkelabels_shipping_gls_private'] );
            unset( $rates['pakkelabels_shipping_gls_business'] );
            unset( $rates['pakkelabels_shipping_gls'] );
            unset( $rates['pakkelabels_shipping_pdk'] );
            unset( $rates['pakkelabels_shipping_postnord_private'] );
            unset( $rates['pakkelabels_shipping_postnord_business'] );
            // unset( $rates['local_pickup:19'] );
        }
        return $rates;
    }
}

My problem is, that this removes the shipping methods for both the order and recurring order, as you can see on the image.

I need some sort of conditional, so that I can target only the recurring order shipping methods and unset those.

How can I achieve this?


Solution

  • Okay - that was a simple fix. WC()->cart->recurring_carts was the conditional I needed. My code now looks like this:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_methods_woo_sg', 10, 2 );
    function hide_shipping_methods_woo_sg( $rates, $package ) {
    
        $product_id = get_field('product_auto_cart', 'option');
    
        if($product_id){
            $product_cart_id = WC()->cart->generate_cart_id( $product_id );
            $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
    
            if($in_cart && WC()->cart->recurring_carts) {
                unset( $rates['pakkelabels_shipping_dao_direct'] );
                unset( $rates['pakkelabels_shipping_gls_private'] );
                unset( $rates['pakkelabels_shipping_gls_business'] );
                unset( $rates['pakkelabels_shipping_gls'] );
                unset( $rates['pakkelabels_shipping_pdk'] );
                unset( $rates['pakkelabels_shipping_postnord_private'] );
                unset( $rates['pakkelabels_shipping_postnord_business'] );
                // unset( $rates['local_pickup:19'] );
            }
            return $rates;
        }
    }
    

    The above shipping methods are now removed for recurring carts.

    My cart totals now looks like this:

    enter image description here