I am using the following code, which allows me to set a minimal quantity of products in cart to proceed payment
// Allow order only product quantity increment
function s5_unsetting_payment_gateway( $available_gateways ) {
if ( ! is_admin() ) {
$cart_quantity = WC()->cart->get_cart_contents_count();
if ( ( $cart_quantity % 6 ) !== 0 ) {
unset($available_gateways['cod']);
unset($available_gateways['bacs']);
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 's5_unsetting_payment_gateway', 10, 1 );
Is there some option to exclude some specific category?
For example:
I need to set the minimum quantity for one order for 6 pieces of goods (applies to all categories).
For one category of products (ID 2), however, I want that if there are only goods from this category in the basket, set the minimum for the order to 3 pieces.
Any advice?
CodePudding user response:
This answer contains:
- If a product does not belong to the specific category, the minimum quantity will be 6
- If all products do belong to that one category, the minimum quantity will be 3
So you get:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// Term_id
$cat_id = 2;
// Default
$minimum_quantity = 3;
// WC Cart
if ( WC()->cart ) {
// Get total items in cart, counts number of products and quantity per product
$cart_quantity = WC()->cart->get_cart_contents_count();
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// When a product does not belong to the specific category, the if condition will apply
if ( ! has_term( $cat_id, 'product_cat', $cart_item['product_id'] ) ) {
$minimum_quantity = 6;
break;
}
}
// Condition modulo
if ( ( $cart_quantity % $minimum_quantity ) !== 0 ) {
// Bacs
if ( isset( $payment_gateways['bacs'] ) ) {
unset( $payment_gateways['bacs'] );
}
// Cod
if ( isset( $payment_gateways['cod'] ) ) {
unset( $payment_gateways['cod'] );
}
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );