Woocommerce购物车页面默认为一个

时间:2015-07-05 10:13:41

标签: php wordpress woocommerce default

我有我的woocommerce页面销售单个产品。我需要转到购物车页面中默认值为1的购物车页面,以便用户点击

www.test.com/cart

它永远不会说"购物车是空的"! 有办法吗? 我需要做什么?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

听起来就像您想要将产品添加到用户的购物车中,即使他们自己实际上没有添加产品:

  

因此,当用户点击www.test.com/cart时,它永远不会说“购物车是空的”

对于它的价值,我不认为从用户体验的角度来看这是一个奇妙的想法(或者几乎任何其他的)。但是,这是可能的。

警告:此代码在很大程度上未经测试,并且已触发在init上运行(尽管每次访问购物车时它都应该运行)。一般来说,我会反对这一点 - 但它应该做你想做的事情:

function vnm_addThingToCart() {

    //  Check we're not in admin

    if (!is_admin()) {

        //  Only do this on the cart page

        if (is_page('cart')) {
            global $woocommerce;

            //  You're only selling a single product, so let's make sure we're not going to add the same thing over and over

            $myProductID = 22;  //  Or whatever

            $productToAdd = get_product($myProductID);

            //  Make sure the product exists

            if ($productToAdd) {

                //  Make sure there's stock available

                $currentStock = $productToAdd->get_stock_quantity();

                if ($currentStock > 0) {

                    //  Add the product

                    $woocommerce->cart->add_to_cart($myProductID);
                }
            }

        }

    }
}

add_action('init', 'vnm_addThingToCart');