繁体   English   中英

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

[英]Automatically add multiple products to cart on visit 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

因此,如果您只有一种产品,但是我想添加一种以上的产品,则此方法效果很好。 有没有一些PHP知识(和一些WordPress)可以帮助我的人? 提前致谢!

实际上,至少有两种方法可以执行此操作。 您可以多次调用add_to_cart函数,也可以创建一个循环。 两种方法都在这里:

方法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 );
        }
    }
}

方法二

foreach ($articles as $article) {
    WC()->cart->add_to_cart( $article );
}

请注意,您应该创建一个名为articles的新数组,其中包含所需产品的所有ID。 然后让您烦恼的另一件事是检查购物车是否容纳超过0件物品,并检查是否所有物品都在其中。

方法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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM