我正在寻找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' );
Run Code Online (Sandbox Code Playgroud)
我应该为此目的使用哪个钩子?
或者你能给我一些类似问题的相关链接吗?
谢谢