phpwoocommercepayment

Maximun Order Amount for specific payment method in WooCommerce


We have to set a maximum order amount for Check payments in WooCommerce, and we don't want to use plugins. But I can't find something suitable, tried some codes and nothing worked.


Solution

  • Based on Hide cash on delievery for orders over certain amount of money in woocommerce answer, you can adapt it changing 'cod' with 'cheque' payment ID.

    For a maximum order amount based on cart subtotal (for specific payment ID(s)):

    add_filter( 'woocommerce_available_payment_gateways' , 'max_order_amount_for_payment_gateways', 100 );
    function max_order_amount_for_payment_gateways( $payment_gateways ){
        if( is_admin() ) {
            return $payment_gateways; // Not in backend (admin)
        }
        
        $targeted_payment_ids = array('cheque'); // Here define the affected payment method IDs
        $threshold_amount  = 100; // Here define the desired subtotal amount threshold 
        $cart_subtotal = WC()->cart->subtotal;
    
        // Loop through available payment IDs
        foreach ( $payment_gateways as $payment_id => $values ) {
            if( in_array($payment_id, $targeted_payment_ids) && $cart_subtotal >= $threshold_amount ){
                unset($payment_gateways[$payment_id]); // Hide payment method
            }
        }
        return $payment_gateways;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    For a Minimum order amount, simply replace
    $cart_subtotal >= $threshold_amount
    with
    $cart_subtotal < $threshold_amount