如何为“woocommerce_add_to_cart”添加过滤器或挂钩 [英] How to add filter or hook for "woocommerce_add_to_cart"
问题描述
我想添加到购物车的两个产品,一个是原来的(当前)产品,第二个是从下拉列表中。 ('woocommerce_add_to_cart','custome_add_to_cart');
$ cnt = 2
函数custome_add_to_cart(){
global $ woocommerce;
$ cnt = $ cnt + 1;
echo $ cnt。X;
echo $ p_id = $ _ POST ['assessories'];
$ woocommerce-> cart-> add_to_cart($ p_id,1);
$输出:
正如你所看到的在下方输出图像,它在购物车中多次添加相同的下拉项目,但我只想要1个数量添加到购物车。似乎add_to_cart函数运行了很多次。
我应该怎么做,或者如何添加过滤器与传递第二下拉产品作为参数添加到购物车功能?所以我可以添加此产品也在购物车。
您需要找到另一种方法,或者停止在您自己的代码中调用 I want add to cart two product at the same time, one is original (current) product and second is from drop-down list Output:-
As you can see in output image below , it adding same drop-down item many time in cart but i want only 1 quantity to add to cart. it seems that add_to_cart function run many times.
What should i do or how to add filter with passing second drop-down product as parameter to add to cart function ? so i can add this product also in cart. The woocommerce "add_to_cart" functions run the hook "woocommerce_add_to_cart". So, in your code "add_to_cart" is run, which is running "woocommerce_add_to_cart" which runs your code, which runs "add_to_cart", etcetera etcetera... You created a recursive loop. You need to find an alternative way, or stop calling 这篇关于如何为“woocommerce_add_to_cart”添加过滤器或挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ woocommerce-> cart-> add_to_cart($ p_id,1);
。 add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
$cnt=2
function custome_add_to_cart() {
global $woocommerce;
$cnt = $cnt + 1;
echo $cnt."X";
echo $p_id=$_POST['assessories'];
$woocommerce->cart->add_to_cart($p_id, 1);
}
$woocommerce->cart->add_to_cart($p_id, 1);
in your own code.