Sunday, 11 December 2016

How to hide or disable payment gateway in wooCmmerce


//Check if product is available in cart or not.
function is_product_in_cart( $prodids ){
 $product_in_cart = false;
 foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  $product = $cart_item['data'];
      if ( in_array( $product->id, $prodids ) ) {
          $product_in_cart = true;
  }
            
 }
 return $product_in_cart;
}
//  Disable gateway based on product
function payment_gateway_disable_product( $available_gateways ) {
 global $woocommerce;
 //print_r( $available_gateways );
 $prodids=array(14,15);
 if ( isset( $available_gateways['paypal'] ) && is_product_in_cart( $prodids ) ) {
     unset(  $available_gateways['paypal'] );
 }
 return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_product' );