添加到购物车在WooCommerce中经过验证的每种产品的最大数量 [英] Add to cart maximun quantity per product with validation in WooCommerce

查看:46
本文介绍了添加到购物车在WooCommerce中经过验证的每种产品的最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将添加到购物车"按钮的数量限制为每个产品最多10个.我希望客户每个订单购买的每种产品的数量不能超过10个.

I want to restrict add to cart button with a max quantity of 10 per product. I want customers cannot purchase more than 10 quantity per product per order.

这是代码

add_filter( 'woocommerce_add_to_cart_validation', 'restrict_per_product_quantity' );
function restrict_per_product_quantity($cart_item_data) {
    global $woocommerce;
    $item_count = $woocommerce->cart->cart_contents_count;
    if($item_count > 10) {
        wc_add_notice( 'Sorry, Only 10 quantity per product per order is allowed. If you would like to order more please contact support.', 'error' );
        return false;
    }
    return $cart_item_data;
}


问题在于此代码不适用于每种产品,它会检查购物车,如果购物车中有10件商品,则会显示错误.


The issue is this code did not work with per product, it checks the cart, and if there are 10 items in the cart it displays the error.

推荐答案

  • woocommerce_add_to_cart_validation 包含5个可以使用的参数
  • 通过代码中添加的注释进行解释
    • woocommerce_add_to_cart_validation contains 5 parameters that you can use
    • explanation via comments added in the code
    • function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
          // Set max allowed
          $max_allowed = 10;
          
          // Error message
          $message = 'max ' . $max_allowed . ' products allowed';
          
          // quantity > max allowed || elseif = when cart not empty
          if ( $quantity > $max_allowed ) {
              wc_add_notice( __( $message, 'woocommerce' ), 'error' );
              $passed = false;   
          } elseif ( ! WC()->cart->is_empty() ) {
              // Get current product id
              $product_id = $variation_id > 0 ? $variation_id : $product_id;
          
              // Cart id
              $product_cart_id = WC()->cart->generate_cart_id( $product_id );
          
              // Find product in cart
              $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
              
              // True
              if ( $in_cart ) {
                  // Get cart
                  $cart = WC()->cart->get_cart();
      
                  // Current quantity in cart
                  $quantity_in_cart = $cart[$product_cart_id]['quantity'];
                  
                  // Condition: quanitity in cart + new add quantity greater than max allowed
                  if ( $quantity_in_cart + $quantity > $max_allowed ) {           
                      wc_add_notice( __( $message, 'woocommerce' ), 'error' );
                      $passed = false;
                  }
              }
          }
          
          return $passed;
      }
      add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
      

      这篇关于添加到购物车在WooCommerce中经过验证的每种产品的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆