【发布时间】:2020-04-28 23:54:58
【问题描述】:
所以我可以使用 woocommerce 文档中的这个 sn-p,它可以在达到购物车总数时添加免费产品。但是,如果我们的客户不想要产品,他们需要能够将其从购物车中移除(重复客户每次都不想要相同的礼物)。使用此 sn-p,从购物车中删除按钮不适用于免费产品。
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 30; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
``
code above is from here: https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/
【问题讨论】:
-
这很接近@7uc1f3r,但是一旦不再满足阈值或免费礼物要求,这就会删除。即使满足阈值/要求,我也在寻找从购物车中移除的能力。
标签: php wordpress woocommerce