【发布时间】:2016-08-23 08:54:40
【问题描述】:
我不确定这是否是正确的问题,但我仍然没有找到任何解决方案。我正在尝试创建一种方法,当用户转到当前结帐页面以外的页面/站点时,用户的购物车将自动清空。我想知道这是否可能,因为我已经改变了我的定制网店的购买流程。我已经尝试在互联网上搜索并尝试了很多方法,但似乎不起作用。我目前很难理解 WooCommerce 的钩子和函数是如何工作的。
现在,让我告诉你们我的代码是如何从头开始工作的:-
这些是显示整个可用产品的页面代码。如您所见,当用户想要购买产品时,他们会点击购买按钮,我的网店会自动将该产品添加到购物车中。
<?php
// Querying of product information retrieval
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' =>'ASC');
$loop = new WP_Query( $args );
// Display each retrieved product
while ( $loop->have_posts() ) :
$loop->the_post();
// WooCommerce global product variable
global $product;
global $woocommerce;
the_title();
the_excerpt();
echo $product->get_price_html();
$id = $product->id;
?>
/**** Select the product & automatically add to cart ****/
<a href="<?php echo do_shortcode('[add_to_cart_url id="' . $id . '"]'); ?>"><button type="button"> BUY</button></a>
<?php endwhile; ?>
<?php wp_reset_query(); // After the loop ended, quit the custom loop and reset back the main loop
?>
当我的电子商店完成“添加到购物车”过程后,用户将被重定向到结帐页面。它的功能如下。
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
基本上我想说的是,用户在点击购买该产品后会直接重定向到结帐页面。此方法跳过了 Single Product 页面 & Add to Cart 页面。
但现在,我需要帮助确定正确的方法如果用户退出该页面或注销,购物车将自动清空。可能吗?如果没有,请原谅我缺乏知识。
我一直在尝试下面的代码,因为它是我想要它做的最接近的。关闭,但不工作。
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( !is_page('93') ) {
$woocommerce->cart->empty_cart();
}
}
使用上面的功能,每当我想回到首页,或者去其他链接再次点击购买时,购物车就一直在添加。这件事让我失去理智。希望大家能帮帮我。
【问题讨论】:
-
嗨凯达林。你有没有找到解决方案?我需要做同样的事情。
-
@inspirednz 请尝试我的解决方案
标签: wordpress woocommerce cart