【发布时间】:2018-09-19 06:57:56
【问题描述】:
在 Woocommerce 中,我使用 this official code snippet 在客户访问我的网站时自动将产品添加到购物车:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 1234; //replace with your own product id
$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() == $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 );
}
}
}
它工作正常并将产品添加到购物车。
但如果客户不想要,我希望客户提供从购物车中删除此商品的选项。但实际上该产品无法移除。
如何使产品自动添加到购物车中?
【问题讨论】:
-
这个产品是静态的吗?
标签: php wordpress woocommerce product cart