【发布时间】:2017-02-11 06:41:47
【问题描述】:
我正在 WooCommerce 中寻找合适的钩子,因为当达到一定的购物车数量(例如 100 个常规单位)时,我需要将促销产品添加到购物车中。
我也用过钩子'init',但我觉得不对。
这是我的代码:
function add_free_product_to_cart(){
global $woocommerce;
$product_id = 2006;
$found = false;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 )
{
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values )
{
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
if(!$found)
{
$maximum = 100;
$current = WC()->cart->subtotal;
if($current > $maximum){
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
add_action( 'woocommerce_add_to_cart', 'add_free_product_to_cart' );
为此我应该使用哪个钩子?
或者你能给我一个类似问题的相关链接吗?
谢谢
【问题讨论】:
-
对不起这个错误一定是 add_action( 'init', 'add_free_product_to_cart' );
标签: php wordpress woocommerce cart product