Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress Woocommerce在访问产品页面时自动添加到购物车_Wordpress_Woocommerce_Hook Woocommerce - Fatal编程技术网

Wordpress Woocommerce在访问产品页面时自动添加到购物车

Wordpress Woocommerce在访问产品页面时自动添加到购物车,wordpress,woocommerce,hook-woocommerce,Wordpress,Woocommerce,Hook Woocommerce,当用户访问产品页面时,如何自动添加到购物车中, 例如:如果用户访问产品,功能应自动将产品添加到用户购物车中您可以使用WC action hookstemplate\u redirect检查您是否在单个产品页面上。代码将放入活动的theme functions.php文件中 您可以使用两种方式添加产品 使用全球商务 $quantity = 1; $woocommerce->cart->add_to_cart( get_the_ID(), $quantity ); 使用WC() 完整的

当用户访问产品页面时,如何自动添加到购物车中,
例如:如果用户访问产品,功能应自动将产品添加到用户购物车中

您可以使用WC action hooks
template\u redirect
检查您是否在单个产品页面上。代码将放入活动的theme functions.php文件中

您可以使用两种方式添加产品

使用
全球商务

$quantity = 1;
$woocommerce->cart->add_to_cart( get_the_ID(), $quantity );
使用
WC()

完整的代码如下所示

add_action( 'template_redirect','add_product_into_cart_when_visit_product_page' );
function add_product_into_cart_when_visit_product_page(){
    if ( class_exists('WooCommerce') ){
        if( is_product() ){
            global $woocommerce;
            $quantity = 1;
            $woocommerce->cart->add_to_cart( get_the_ID(), $quantity );
        }
    } 
}
注意-请记住,它将在您每次访问页面时添加产品。如果您每次访问同一页面,则它将增加数量。因此,如果您只想添加一次,请使用下面的代码

add_action( 'template_redirect','add_product_into_cart_when_visit_product_page' );
function add_product_into_cart_when_visit_product_page(){
    if ( class_exists('WooCommerce') ){
        global $woocommerce;
        if( is_product() ){
            $product_id = get_the_ID();
            if( !is_product_already_in_cart( $product_id ) ){
                $quantity = 1;
                $woocommerce->cart->add_to_cart( $product_id, $quantity );
            }
        }
    } 
}

function is_product_already_in_cart( $product_id ){

    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $in_cart         = WC()->cart->find_product_in_cart( $product_cart_id );
  
    if ( $in_cart ) {
        return true;
    }

    return false;

}
add_action( 'template_redirect','add_product_into_cart_when_visit_product_page' );
function add_product_into_cart_when_visit_product_page(){
    if ( class_exists('WooCommerce') ){
        global $woocommerce;
        if( is_product() ){
            $product_id = get_the_ID();
            if( !is_product_already_in_cart( $product_id ) ){
                $quantity = 1;
                $woocommerce->cart->add_to_cart( $product_id, $quantity );
            }
        }
    } 
}

function is_product_already_in_cart( $product_id ){

    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $in_cart         = WC()->cart->find_product_in_cart( $product_cart_id );
  
    if ( $in_cart ) {
        return true;
    }

    return false;

}