简体   繁体   English

根据 WooCommerce 中的产品属性添加到购物车验证

[英]Add to cart validation based on product attribute in WooCommerce

I'm trying to add a filter when a customer adds a product to the cart, to allow it or not.我正在尝试在客户将产品添加到购物车时添加过滤器,无论是否允许。

We just need to compare one attribute of the WooCommerce products.我们只需要比较 WooCommerce 产品的一个属性。

  • If cart is empty : Add to cart ok如果购物车是空的:添加到购物车确定
  • If cart has 1 or more items : check Attribute XXX value of products如果购物车有 1 个或多个项目:检查产品的属性 XXX 值
  • If Attribute value is the same : Add to cart ok如果属性值相同:添加到购物车 ok
  • If Attribute value is different : Add to cart denied and message will display如果属性值不同:添加到购物车被拒绝并显示消息

I have this code so far, but it doesn't work well at all, and I'm unsure why?到目前为止,我有这段代码,但它根本无法正常工作,我不确定为什么?

// Check Products added to cart for same vendor
function so_validate_add_cart_item( $passed, $product_id ) {

  global $woocommerce;
  $items = $woocommerce->cart->get_cart();

  foreach($items as $item => $values)
        {
          $_product =  wc_get_product( $values['data']->get_id());
          $prod1_vendeur[] = $_product->get_attribute( 'pa_vendeur' );
        }

  $newproduct = wc_get_product( $product_id );
  $prod2_vendeur = $newproduct->get_attribute( 'pa_vendeur' );

  if (isset($prod1_vendeur ))
  {
    if ( $prod1_vendeur[0] != $prod2_vendeur )
      {
        $passed = false;
        wc_add_notice( 'Error message' , 'notice' );
      }
  }
  return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );

Any help is more than appreciated.任何帮助都非常感谢。

  • While you specify that the woocommerce_add_to_cart_validation hook contains 5 arguments, you pass only 2当您指定woocommerce_add_to_cart_validation挂钩包含 5 个参数时,您只传递了 2 个
  • global $woocommerce; is not necessary, use WC() instead没有必要,请改用WC()

So you get:所以你得到:

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {   
    // Setting
    $attribute = 'pa_vendeur';
    
    // Real product ID
    $product_id = $variation_id > 0 ? $variation_id : $product_id;
    
    // Get product
    $product = wc_get_product( $product_id );
    
    // Get the product attribute value
    $product_attribute = $product->get_attribute( $attribute );
    
    // Initialize
    $flag = false;
    
    // WC Cart
    if ( WC()->cart ) {
        // Get cart
        $cart = WC()->cart;
        
        // If cart is NOT empty
        if ( ! $cart->is_empty() ) {
            // Loop through cart items
            foreach( $cart->get_cart() as $cart_item ) {
                // Get the product attribute value
                $cart_item_attribute = $cart_item['data']->get_attribute( $attribute );
                
                // NOT equal
                if ( $cart_item_attribute != $product_attribute ) {
                    // Flag becomes true
                    $flag = true;
                    
                    // Break loop
                    break;  
                }
            }
        }
    }
    
    // True
    if ( $flag ) {
        // Display an error message
        wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );
        
        $passed = false;
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );

暂无
暂无

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

相关问题 根据购物车页面中的属性值和产品数量,在 WooCommerce 中添加到购物车验证 - Add to cart validation in WooCommerce based on attribute value and product quantity from the cart page 根据Woocommerce中的特定产品属性值更改购物车商品价格 - Change cart item prices based on specific product attribute value in Woocommerce 在 WooCommerce 中通过验证将每个产品添加到购物车的最大数量 - Add to cart maximun quantity per product with validation in WooCommerce 针对woocommerce中的特定产品添加到购物车验证挂钩 - Targeting a particular product in woocommerce add to cart validation hook 隐藏 Woocommerce 产品变体中特定属性值的添加到购物车按钮 - Hide Add to Cart button in Woocommerce product variations for a specific attribute value 基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮 - Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type 根据 WooCommerce 产品自定义库存状态禁用添加到购物车按钮 - Disable add to cart button based on WooCommerce product custom stock status 根据Woocommerce中购物车的数量增加产品价格的额外成本 - Add an extra cost to product price based on cart item count in Woocommerce 根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本 - Change Add To Cart button text based on parent product categories in Woocommerce 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品 - Add or remove a free product based on WooCommerce cart total and month range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM