钩子添加到购物车中的Woocommerce [英] Hooks around add to cart for Woocommerce

查看:164
本文介绍了钩子添加到购物车中的Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WC Marketplace上运营WooCommerce商店.我想通过下面的挂钩实现的目标是,如果购物篮中已经有其他供应商的产品,则可以防止将新商品添加到购物篮中.例如如果购物者将来自卖方y的产品x添加到他的购物篮中,如果他们要添加来自卖方b的产品a,则不会添加该项目,并且会通知用户该错误.

I am running a WooCommerce store with WC Marketplace. What I am trying to achieve with the hook below is to prevent a new item being added to a basket if there is already a product in the basket from a different vendor. e.g. If a shopper adds product x from vendor y to his basket, if they were to add product a from vendor b, then the item will not be added and the user will be informed of the error.

我有2个问题:
-首先,钩子何时运行,是在主函数触发之前还是之后?我对函数woocommerce_add_to_cart感兴趣.所以我想知道在函数woocommerce_add_to_cart运行之后还是之前,钩子会触发. -我的第二个问题是,我在下面附上了挂钩,您认为这行得通吗?

I have 2 questions:
- firstly when does a hook run, is it before the main function fired or after? I have a hook for the function woocommerce_add_to_cart. So I want to know will the hook fire after the function woocommerce_add_to_cart runs or before.
- My second question is, I have attached the hook below, in your opinion would this work?

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

致谢

推荐答案

以下是 add_to_cart()方法:

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

  1. 验证过滤器挂钩woocommerce_add_to_cart_validation
  2. 项目数量更改过滤器挂钩woocommerce_add_to_cart_quantity (不带ajax)
  3. 项目数据更改过滤器挂钩woocommerce_add_cart_item_data (不带ajax)
  4. 以及与单独出售" 产品相关的其他一些信息(请参见
  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)将商品添加到购物车后:

  1. 更换购物车物品过滤钩woocommerce_add_cart_item
  2. 添加事件,动作挂钩woocommerce_add_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. 挂钩位置::它位于WC_Cart add_to_cart() 方法(在源代码末尾).
  3. 触发钩子时:WC_Cart add_to_cart() 方法.
  4. 目的是什么:执行此方法(事件)时执行一些自定义代码.
  1. As you can see now woocommerce_add_to_cart is not a function but only an action hook.
  2. Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
  3. When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
  4. What is the purpose: To execute some custom code when this method is executed (event).


关于您的代码:
最好使用专用的过滤器挂钩 woocommerce_add_to_cart_validation ,该挂钩将阻止想要在购物车中添加新商品的客户,如果购物车中已有来自其他供应商的商品,则显示 > 可选 一条自定义消息:


Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:

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文件或任何插件文件中.

经过测试,可以正常工作.

Tested and works.

这篇关于钩子添加到购物车中的Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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