0
我正在一个网上商店测试案例,我想要的是自动添加项目/产品的访问购物车。所以当我在世界各地找到相同的代码时,我搜索了类似的东西。自动添加多个产品到访问购物车WooCommerce
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action('template_redirect', 'add_product_to_cart');
function add_product_to_cart() {
if (! is_admin()) {
$product_id = 64;
$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->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);
}
}
}
来源:
https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300
所以,如果你只有一个产品,但是我想补充的不仅仅是1个产品更多能正常工作。有人有一些PHP知识(和一些WordPress)可以帮助我吗?提前致谢!