[英]Automatically add multiple products to cart on visit WooCommerce
I am working on a webshop test case and what I would like is to add automatically items/products to the cart on visit. 我正在一个网上商店测试用例上工作,我想在访问时将商品/产品自动添加到购物车中。 So I searched for something like that when I found the same code over and over everywhere.
因此,当我到处遍地发现相同的代码时,我便搜索了类似的内容。
/*
* 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 );
}
}
}
From: 从:
https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300 https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300
So this works fine if you have only one product but I would like to add more than just 1 product. 因此,如果您只有一种产品,但是我想添加一种以上的产品,则此方法效果很好。 Is there someone with some PHP knowledge (and a some WordPress) that can help me out?
有没有一些PHP知识(和一些WordPress)可以帮助我的人? Thanks in advance!
提前致谢!
There are actually two ways you can do this, at least. 实际上,至少有两种方法可以执行此操作。 You can either call the
add_to_cart
function more than once, or create a loop. 您可以多次调用
add_to_cart
函数,也可以创建一个循环。 Both ways are down here: 两种方法都在这里:
Method 1 方法1
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 );
WC()->cart->add_to_cart( $product_id2 );
WC()->cart->add_to_cart( $product_id3 );
WC()->cart->add_to_cart( $product_id4 );
}
}
}
Method 2 方法二
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
Do note that you should create a new array called articles
holding all the IDs from the desired products. 请注意,您应该创建一个名为
articles
的新数组,其中包含所需产品的所有ID。 Another thing that is going to bother you then is checking whether the cart holds more than 0 items, and checking if all of them are in there. 然后让您烦恼的另一件事是检查购物车是否容纳超过0件物品,并检查是否所有物品都在其中。
Method 2 would look something like this: 方法2看起来像这样:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$articles = array(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 (($key = array_search($_product->id, $articles)) !== false)
unset($articles[$key]);
}
// if product not found, add it
if ( count($articles) > 0 ) {
foreach ($articles as $article) {
WC()->cart->add_to_cart($article);
}
}
} else {
// if no products in cart, add it
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.