phpwordpresswoocommercepaymentcheckout

Hide shipping and payment methods in WooCommerce


I'm building a WooCommerce e-shop and I need to tweak my checkout page by doing the following:

  1. Hide a certain shipping method (only one) if the order total > 100€.

  2. Hide the cash on delivery payment method if local pickup is selected.

Does anyone know how to do that? I have the Code Snippets plugin so I can easily add any custom code.


Solution

  • To hide specific shipping method based on the cart total, you can use below code snippet. You need to update your shipping method name in the code.

    Disable shipping method as per cart total

    Add this snippet in your theme's functions.php file or custom plugin file.

    add_filter( 'woocommerce_package_rates', 'shipping_based_on_price', 10, 2 );
    
    function shipping_based_on_price( $rates, $package ) {
    
        $total = WC()->cart->cart_contents_total;
        //echo $total;
        if ( $total > 100 ) {
    
            unset( $rates['local_delivery'] ); // Unset your shipping method
    
        }
        return $rates;
    
    }
    

    Disable Payment Gateway For Specific Shipping Method

    Use below code snippet. Update code as per your payment method & shipping method.

    add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );
    
    function x34fg_gateway_disable_shipping( $available_gateways ) {
    
        global $woocommerce;
    
        if ( !is_admin() ) {
    
            $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    
            $chosen_shipping = $chosen_methods[0];
    
            if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
                unset( $available_gateways['cod'] );
            }
    
        }
    
    return $available_gateways;
    
    }