woocommerce访问时自动将产品添加到购物车 - 错误

时间:2014-07-17 11:12:10

标签: php woocommerce

我正在尝试实施以下解决方案:

/*
 * goes in theme functions.php or a custom plugin
 **/
// add item to cart on visit
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        global $woocommerce;
        $product_id = 64;
        $found = false;
        //check if product already in cart
        if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
            foreach ( $woocommerce->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 )
                $woocommerce->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            $woocommerce->cart->add_to_cart( $product_id );
        }
    }
}

从这里: http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/

但它会引发错误:

  

抱歉,此产品无法购买。

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

也许您的产品库存数量为零或产品不存在。请检查一下。

相关问题