【发布时间】:2021-06-12 00:36:11
【问题描述】:
我正在处理一个项目,该项目需要在特定促销月份将免费商品添加到购物车。因此,我需要将产品价值设为 0.00 并在特定促销月份将其自动添加到购物车。到目前为止,当添加到购物车的总数达到一定数量时,我已经自动添加了产品
/*
* Automatically adding the product to the cart when cart total amount reach to $500.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 500;
if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 12989; // Product Id of the free product which will get added to cart
$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->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
...
【问题讨论】:
-
谢谢@LoicTheAztec,你救了我的命,效果很好,当客户将可变产品添加到购物车时,我还需要添加一个促销弹出窗口,这可能吗?什么是sn-p?非常感谢您的热心帮助...
-
@LoicTheAztec 感谢您让我知道,我已经接受了答案,也赞成它。谢谢
标签: php wordpress woocommerce cart hook-woocommerce