[英]Disable payment gateway based on number of items and product category in WooCommerce cart
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).我需要为6件商品设置一个订单的最小数量(适用于所有类别)。
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.但是,对于一类产品(ID 2),我希望如果购物篮中只有该类别的商品,则将订单的最小数量设置为 3 件。
Any advice?有什么建议吗?
This answer contains:这个答案包含:
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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.