访问WooCommerce

时间:2017-04-03 12:38:15

标签: php wordpress

我正在开发一个网店测试案例,我希望在访问时自动将项目/产品添加到购物车。所以当我在各处找到相同的代码时,我搜索了类似的东西。

/*
 * 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)可以帮助我?提前致谢!

1 个答案:

答案 0 :(得分:2)

至少有两种方法可以做到这一点。您可以多次调用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 );
        }
    }
}

方法2

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