简体   繁体   English

在访问WooCommerce时自动将多种产品添加到购物车

[英]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.

相关问题 在Woocommerce中访问时自动添加到购物车最新发布的产品 - Automatically add to cart latest published product on visit in Woocommerce 根据范围条件自动将产品添加到WooCommerce购物车 - Automatically add products to WooCommerce cart based on range conditions 如果 WooCommerce 购物车至少包含 X 个产品,则自动添加百分比折扣 - Automatically add a percentage discount if WooCommerce cart contains at least X products 使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车 - Creating dropdown with Woocommerce products and automatically add product to cart based on selection WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart Woocommerce多个产品无法购物? - Woocommerce multiple products to cart not working? WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout 无法在购物车中的WooCommerce中添加多个可变订阅产品 - Unable to add multiple variable subscription products in WooCommerce in Cart WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations 将多种产品添加到购物车 - Add multiple products to cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM