钩子周围添加到Woocommerce购物车

时间:2017-11-28 12:35:51

标签: php wordpress woocommerce cart hook-woocommerce

我正在与WC Marketplace一起经营一家WooCommerce商店。我想通过下面的钩子实现的目的是防止新项目被添加到篮子中,如果篮子中已有来自不同供应商的产品。例如如果购物者将产品x从供应商y添加到他的购物篮中,如果他们要从供应商b添加产品a,则不会添加该商品,并且将通知用户该错误。

我有两个问题:
- 首先是挂钩运行的时间,是在主要功能被触发之前还是之后?我有一个函数woocommerce_add_to_cart的钩子。所以我想知道函数woocommerce_add_to_cart运行之后或之前的挂钩是什么 - 我的第二个问题是,我已经附上了下面的钩子,你觉得这会有用吗?

function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { 
    $same_vendor = 1;

    $empty = WC_Cart::is_empty();

    //If there is an item in the cart then,
    if (!$empty) {
        //Get the VendorId of the product being added to the cart.
        $vendor = get_wcmp_product_vendors($product_id);
        $vendor_id = $vendor->id;

        foreach( WC()->cart->get_cart() as $cart_item ) {
            //Get the vendor Id of the item
            $cart_product_id = $cart_item['product_id'];
            $cart_vendor = get_wcmp_product_vendors($product_id);
            $cart_vendor_id = $cart_vendor->id;

            //If two products do not have the same Vendor then set $same_vendor to 0
            if($vendor_id !== $cart_vendor_id) {
                $same_vendor = 0;
            }
        }

        if ($same_vendor === 0) {
            WC()->cart->remove_cart_item( $cart_item_key );
            //How do I show a message to tell the customer.
        }
    }
}

此致

1 个答案:

答案 0 :(得分:2)

  

以下是WC_Cart add_to_cart() method中涉及的钩子:

     

A)将商品添加到购物车之前:

     
      
  1. 验证过滤器挂钩woocommerce_add_to_cart_validation
  2.   
  3. 商品数量更改过滤器挂钩woocommerce_add_to_cart_quantity (不带ajax)
  4.   
  5. 项目数据更改过滤器挂钩woocommerce_add_cart_item_data (不带ajax)
  6.   
  7. 以及与"单独销售" 产品相关的其他一些产品(请参阅here
  8.         

    A)将商品添加到购物车后:

         
        
    1. 更改购物车项目过滤器挂钩woocommerce_add_cart_item
    2.   
    3. 添加一个事件,动作挂钩woocommerce_add_to_cart
    4.   

在您的情况下要明确:

  1. 正如您所看到的,woocommerce_add_to_cart不是一个功能,而只是一个动作挂钩。
  2. 隐藏位置:它位于WC_Cart add_to_cart()方法内(位于源代码末尾)。
  3. 当挂钩被触发时:执行WC_Cart add_to_cart()方法后,它会被解雇。
  4. 目的是什么:执行此方法时执行某些自定义代码 (事件)
  5. 关于您的代码:
    最好使用专用过滤器挂钩 woocommerce_add_to_cart_validation ,如果已有来自其他供应商的购物车中的产品,则会阻止想要将新商品添加到购物车的客户 可选 自定义消息:

    add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
    function filter_add_to_cart_validation( $passed, $product_id, $quantity ) { 
        if ( WC()->cart->is_empty() ) return $passed;
    
        // Get the VendorId of the product being added to the cart.
        $current_vendor = get_wcmp_product_vendors($product_id);
    
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Get the vendor Id of the item
            $cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);
    
            // If two products do not have the same Vendor
            if( $current_vendor->id != $cart_vendor->id ) {
                // We set 'passed' argument to false
                $passed = false ;
    
                // Displaying a custom message
                $message = __( "This is your custom message", "woocommerce" );
                wc_add_notice( $message, 'error' );
                // We stop the loop
                break; 
            }
        }
        return $passed;
    }
    

    代码放在活动子主题(或活动主题)的function.php文件中或任何插件文件中。

    经过测试和工作。

相关问题