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)可以幫助我嗎?提前致謝!