Hooks around 添​​加到 Woocommerce 的购物车

Hooks around add to cart for Woocommerce

我运行正在使用 WC Marketplace 开设 WooCommerce 商店。我试图通过下面的挂钩实现的是,如果篮子中已经有来自不同供应商的产品,则防止将新项目添加到篮子中。例如如果购物者将供应商 y 的产品 x 添加到他的购物车,如果他们要添加供应商 b 的产品 a,则该项目将不会被添加,并且用户将被告知错误。

我有两个问题:
- 首先什么时候挂钩 运行,是在主函数触发之前还是之后?我有一个函数 woocommerce_add_to_cart 的钩子。所以我想知道钩子会在函数 woocommerce_add_to_cart 运行s 之后或之前触发。
- 我的第二个问题是,我在下面附上了挂钩,您认为这行得通吗?

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.
        }
    }
}

此致

Here below are the hooks involved in WC_Cart add_to_cart() method:

A) Before an item is added to cart:

  1. Validation filter hook woocommerce_add_to_cart_validation
  2. Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
  3. Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
  4. and some others related to "sold individually" products (see here)

A) After an item is added to cart:

  1. Change cart Item filter hook woocommerce_add_cart_item
  2. Add an event, action hook woocommerce_add_to_cart

要明确你的情况:

  1. 正如您现在所见woocommerce_add_to_cart 不是一个函数,只是一个动作挂钩。
  2. Hook位置:位于WC_Cartadd_to_cart()方法内部(源码末尾)
  3. 当钩子被触发时:一旦WC_Cartadd_to_cart()方法被执行就会被触发。
  4. 目的是什么:执行一些自定义代码当执行此方法时(事件).

关于您的代码:
最好使用专用过滤器挂钩 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 文件或任何插件文件。

已测试并有效。