我跳过了我的课程会员网站上的购物车,所以“立即购买”可以直接带你去结账。
但是,如果您在单击“立即购买”时已登录,然后退出而不购买,则下次登录时,该商品将保留在购物车中。如果你在下一次访问时去购买同样的商品,它会说“这个商品已经在你的购物车里了”,但因为我把购物车藏在了前端,所以他们无法访问它。
有没有可能在WooCommerce重新加载页面时清空购物车,这样当用户点击“立即购买”时,购物车就会直接结账?
发布于 2018-02-04 16:28:20
看起来你们是在单独销售产品。在这个解决方案中,我们绕过了过滤器钩子woocommerce_add_to_cart_sold_individually_found_in_cart。当$found_in_cart为true时,用户会收到消息“此商品已在您的购物车中”。这就是我们将数量重置为1的原因。有关更多详细信息,请查看https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1064
function op_bypass_add_to_cart_sold_individually_found_in_cart( $found_in_cart, $product_id ) {
if ( $found_in_cart ) {
$cart_contents = WC()->cart->get_cart_contents();
foreach ( $cart_contents as $key => $item ) {
if ( $product_id === $item['product_id'] ) {
WC()->cart->set_quantity( $key, 1 );
break;
}
}
return false;
}
return $found_in_cart;
}
add_filter( 'woocommerce_add_to_cart_sold_individually_found_in_cart', 'op_bypass_add_to_cart_sold_individually_found_in_cart', 10, 2 );发布于 2018-02-21 23:11:41
在我的例子中,答案@obiPlabon中的“WC()->cart->set_quantity( $key,1 );”不能正常工作,所以我简化了函数。
由于$found_in_cart只在购物车中单独售出的产品数量超过1时才会启动,因此我决定简化代码,只需将其重定向到结帐页面即可。
if ( $found_in_cart ) {
global $woocommerce;
wp_redirect( wc_get_checkout_url() );
exit;
}https://stackoverflow.com/questions/48605504
复制相似问题