php 如何为“woocommerce_add_to_cart”添加过滤器或挂钩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28365050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add filter or hook for "woocommerce_add_to_cart"
提问by luckyamit
I want add to cart two product at the same time, one is original (current) product and second is from drop-down list
我想同时添加两个产品到购物车,一个是原始(当前)产品,第二个来自下拉列表
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);
}
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.
输出:- 正如您在下面的输出图像中看到的,它在购物车中多次添加相同的下拉项目,但我只想将 1 个数量添加到购物车。似乎 add_to_cart 函数运行了很多次。我应该怎么做或如何通过将第二个下拉产品作为参数添加到购物车功能来添加过滤器?所以我也可以在购物车中添加这个产品。
回答by alev
This should work:
这应该有效:
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
global $woocommerce;
$product_id = $_POST['assessories'];
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
Based on following source: https://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
基于以下来源:https: //docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
回答by Niels van Renselaar
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.
woocommerce "add_to_cart" 函数运行钩子 "woocommerce_add_to_cart"。所以,在你的代码中运行“add_to_cart”,它运行“woocommerce_add_to_cart”,它运行你的代码,它运行“add_to_cart”等等......你创建了一个递归循环。
You need to find an alternative way, or stop calling $woocommerce->cart->add_to_cart($p_id, 1);
in your own code.
您需要找到替代方法,或者停止调用$woocommerce->cart->add_to_cart($p_id, 1);
您自己的代码。
回答by Tofandel
What you might be looking for is a variable productwith some attributes!
您可能正在寻找的是具有某些属性的可变产品!
Anyway if really you want to do that then you just need the remove_action function :
无论如何,如果你真的想这样做,那么你只需要 remove_action 函数:
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
$p_id=$_POST['assessories'];
remove_action('woocommerce_add_to_cart', __FUNCTION__);
WC()->cart->add_to_cart( $p_id );
}
This prevents the action from looping indefinitely and is pretty simple.. So it will be added only once for that product. You might want to get the added to cart quantity and give it as a second parameter to the WC()->cart->add_to_cart
function so they are both the same quantity
这可以防止操作无限循环并且非常简单..所以它只会为该产品添加一次。您可能希望获得添加到购物车的数量并将其作为WC()->cart->add_to_cart
函数的第二个参数,以便它们的数量相同
The __FUNCTION__
is a magic PHP tag just giving you the name of the current fucnction as a string, si if the function name is not the same it will still work
这__FUNCTION__
是一个神奇的 PHP 标签,只是将当前功能的名称作为字符串提供给您,如果函数名称不同,它仍然可以工作
回答by user28231
This might be old, but have you tried unsetting the assessories param after adding to cart?, this would break the loop.
这可能很旧,但是您是否尝试在添加到购物车后取消设置评估参数?,这会打破循环。
function custome_add_to_cart() {
global $woocommerce;
if(isset($_POST['assessories'])){
$cnt = $cnt + 1;
echo $cnt . "X";
echo $p_id = $_POST['assessories'];
$woocommerce->cart->add_to_cart($p_id, 1);
unset($_POST['assessories']);
}
}