WooCommerce:当访问指定页面时自动添加指定产品到购物车

Posted by: Fengjiajun Comments: 0

WooCommerce:当访问指定页面时自动添加指定产品到购物车

WooCommerce:当访问指定页面时自动添加指定产品到购物车:

function fjj_add_product_to_cart_on_page_id_load() {
   $product_id = 21874;//产品ID       
   if ( is_page( 19473 ) ) {//页面ID    
      WC()->cart->empty_cart();
      WC()->cart->add_to_cart( $product_id ); 
   }
}
add_action( 'wp', 'fjj_add_product_to_cart_on_page_id_load' );