• Resolved DamirCalusic

    (@webkreativ)


    Hi,

    I have 4 shipping methods in WooCommerce that are active. All 4 methods are fetched to the Vipps Checkout.

    Now I have B2B customers and when a B2B customer is loggedin I need to disable a shipping method in the Vipps Checkout from beeing choosable.

    In this case I would like to disable let say “Local pickup” from beeing added to the Vipps Checkout.

    I have all code ready for checking if a customer is a B2B or a normal B2C customer.

    Is there a filter/action/hook that I can use to achieve my goal by disabling a shipping method from beeing added to the Vipps Checkout?

    • This topic was modified 11 months, 1 week ago by DamirCalusic.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Iver Odin Kvello

    (@iverok)

    You can use this filter to determine if you are inside Vipps Checkout:

    apply_filters('woo_vipps_is_vipps_checkout', false)

    To filter away any methods/rates you don’t want in there, you can either use a filter on ‘woocommerce_package_rates’ which takes a list of WC_Shipping_Rate objects where you can check for instance the method_id of the rate, or you can directly add filters to

    "woocommerce_shipping_{$shipping_method_id}_is_available"

    — family of filters. Just return false if the above value is false.

    Thread Starter DamirCalusic

    (@webkreativ)

    Hi,

    Thank you for a quick reply. I was testing this code below and this code only removes the local pickup shipping method from WooCommerce default checkout and not from the Vipps Checkout. Is there any other filter/action I could use that Vipps Checkout uses to fetch the shipping methods?

    function filter_local_pickup_shipping_method( $rates, $package ) {
        
      if ( is_user_logged_in() && get_user_meta( get_current_user_id(), 'b2b_user', true ) === 'true' ) {
            
            foreach ( $rates as $rate_id => $rate ) {
               
                if ( 'local_pickup' === $rate->method_id ) {
                    unset( $rates[ $rate_id ] );
                    break;
                }
            }
        }
    
        return $rates;
    }
    
    add_filter( 'woocommerce_package_rates', 'filter_local_pickup_shipping_method', 10, 2 );
    
    Plugin Author Iver Odin Kvello

    (@iverok)

    When calculating shipping methods for Vipps Checkout, except if using static shipping, the user will never be “logged in” as such, since the callback is actually coming from Vipps, and not from the user. If you need to check the users’ capabilites, you would probably need to retrieve the user by email from the user database.

    You should be able to remove the rate for Vipps Checkout specifically by doing

    $is_checkout = apply_filters(('woo_vipps_is_vipps_checkout', false);

    and modifying your filter to check for “$is_checkout || ...“.

    As mentioned you can also do something like (untested!):

    add_filter('woocommerce_shipping_local_pickup_is_available', function ($ok) {
        if (apply_filters('woo_vipps_is_vipps_checkout', false)) return false;
        return $ok;
    });
         
    Thread Starter DamirCalusic

    (@webkreativ)

    Thank you very much for excellent feedback. Do you know the ID for Porterbuddy and Posten?

    I tried the following code but Porterbuddy and Posten cannot be removed.

    add_filter('woocommerce_shipping_vipps_checkout_porterbuddy_is_available', function ($ok) {
    	if (apply_filters('woo_vipps_is_vipps_checkout', false)){
    		return false;
    	}
    	
    	return $ok;
    });
    
    add_filter('woocommerce_shipping_porter_buddy_is_available', function ($ok) {
    	if (apply_filters('woo_vipps_is_vipps_checkout', false)){
    		return false;
    	}
    	
    	return $ok;
    });
    Plugin Author Iver Odin Kvello

    (@iverok)

    Not sure 100% I understand, but for the Vipps Checkout specific methods there is another filter available also:

    apply_filters('vipps_checkout_shipping_available', $ok, $shipping_method, $package);

    Ignoring $package you can test the value of $shipping_method->id which will be vipps_checkout_porterbuddy and vipps_checkout_posten.

    The basic filters 'woocommerce_shipping_vipps_checkout_porterbuddy_is_available' and 'woocommerce_shipping_vipps_checkout_posten_is_available' should work too.

    Thread Starter DamirCalusic

    (@webkreativ)

    Thank you for the answers. That worked perfectly.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Disable shipping methods from Vipps Checkout’ is closed to new replies.