V
V
Vladislav Prubnyak2018-02-07 14:56:40
WordPress
Vladislav Prubnyak, 2018-02-07 14:56:40

How to hide gateway in woocommerce if order amount is less than certain value?

How to make the payment gateway unavailable in woocommerce if the order amount is less than a certain amount, ideally, the item on the /checkout page would be shown, but it could not be selected and a window would appear that needs to be added by amount, maybe there are some plugins?
I found this, but I don't know how to merge it into a working version

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    // Set this variable to specify a minimum order value
    $minimum = 100;
 
    if ( WC()->cart->total < $minimum ) {
 
        if( is_cart() ) {
 
            wc_print_notice(
                sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
                    wc_price( $minimum ),
                    wc_price( WC()->cart->total )
                ), 'error'
            );
 
        } else {
 
            wc_add_notice(
                sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
                    wc_price( $minimum ),
                    wc_price( WC()->cart->total )
                ), 'error'
            );
 
        }
    }
 
}

add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {

    // Здесь определяется количество товара
    $qty_limit = 15;
    $limit_reached = false;

    // Перебор по каждому элементу в корзине
    foreach(WC()->cart->get_cart() as $cart_item){
        if($cart_item['quantity'] > $qty_limit ){
            $limit_reached = true;
            break;
        }
    }
    if($limit_reached){
        // Здесь установить способ оплаты
        unset($available_gateways['cod']);
        unset($available_gateways['bacs']);
    }
    return $available_gateways;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Prubnyak, 2018-02-09
@Vaprubnyak

Problem solved thanks OKyJIucT
Working code:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

$limit_reached = false;

function wc_minimum_order_amount() 
{
  // Задать переменную, чтобы указать минимальное значение заказа
  $minimum = 1000;
  
  if ( WC()->cart->total < $minimum ) {
    $limit_reached = true;
    
  }
  return $limit_reached;
}

function unsetting_payment_gateways( $available_gateways ) 
{	
  $limit_reached = wc_minimum_order_amount();
  if ($limit_reached) {			
      // Здесь установить способ оплаты
      unset($available_gateways['cod']);
  }
  
  return $available_gateways;
}
add_action('woocommerce_available_payment_gateways', 'unsetting_payment_gateways');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question