简体   繁体   English

根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关

[英]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:这个答案包含:

  • If a product does not belong to the specific category, the minimum quantity will be 6如果产品不属于特定类别,则最小数量为 6
  • If all products do belong to that one category, the minimum quantity will be 3如果所有产品都属于该类别,则最小数量将为 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 如果购物车中有特定产品,请删除付款网关(Woocommerce) - Remove payment gateway if specific product is in the cart (Woocommerce) 使用 WooCommerce 检查购物车项目中的产品类别 - Check for Product Category in cart items with WooCommerce 禁用基于 WooCommerce 购物车总数的付款方式 - Disable payment methods based on WooCommerce cart total 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物 - Disable shopping when an item from a specific product category is in cart in Woocommerce 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages 限制购物车项目来自 WooCommerce 中的同一产品类别 - Restricting cart items to be from the same product category in WooCommerce 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM