我正在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)在将商品添加到购物车之前:
A)将商品添加到购物车后:
要明确您的情况:
关于您的代码: 最好使用专用的过滤器挂钩 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
发布评论