钩子添加到购物车中的Woocommerce

编程入门 行业动态 更新时间:2024-10-11 07:35:35
本文介绍了钩子添加到购物车中的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)在将商品添加到购物车之前:

  • 验证过滤器挂钩woocommerce_add_to_cart_validation
  • 项目数量更改过滤器挂钩woocommerce_add_to_cart_quantity (不带ajax)
  • 项目数据更改过滤器挂钩woocommerce_add_cart_item_data (不带ajax)
  • 以及与单独出售" 产品相关的其他一些信息(请参见此处)
  • Validation filter hook woocommerce_add_to_cart_validation
  • Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
  • Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
  • and some others related to "sold individually" products (see here)
  • A)将商品添加到购物车后:

  • 更换购物车物品过滤钩woocommerce_add_cart_item
  • 添加事件,动作挂钩woocommerce_add_to_cart
  • Change cart Item filter hook woocommerce_add_cart_item
  • Add an event, action hook woocommerce_add_to_cart
  • 要明确您的情况:

  • 您现在可以看到 woocommerce_add_to_cart 不是功能,而只是动作挂钩.
  • 挂钩位置::它位于WC_Cart add_to_cart() 方法(在源代码末尾).
  • 触发钩子时:WC_Cart add_to_cart() 方法.
  • 目的是什么:执行此方法(事件)时执行一些自定义代码.
  • As you can see now woocommerce_add_to_cart is not a function but only an action hook.
  • Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
  • When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
  • 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

    本文发布于:2023-11-24 21:54:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1626998.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:钩子   车中   Woocommerce

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!