Woocommerce自动添加到购物车:产品ID

时间:2015-03-08 19:40:13

标签: woocommerce

因此,我发现此代码段用于在用户访问产品页面时自动添加产品:

/*
 * Add item to cart on visit
 */
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 =>   $value
 ) {
                            $_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 );
            }
    }
}
add_action( 'init', 'add_product_to_cart' );

现在,它只适用于产品" id = 64。"如何更改它以便添加任何产品。

谢谢

0 个答案:

没有答案