[英]Add a button in checkout to empty cart and redirect to shop page in Woocommerce
I would like to have a cancel order button behind/under the checkout button in Woocommerce. 我想在Woocommerce的结帐按钮后面/下面有一个取消订单按钮。 Then after a customer click this button, the shopping cart would be empty, redirect to shop page.
然后在客户点击此按钮后,购物车将为空,重定向到商店页面。
I tried several things in the template with the woocommerce_cancelled_order in template form-checkout.php
. 我使用模板
form-checkout.php
的woocommerce_cancelled_order在模板中尝试了几个方法。
But I cannot figure this out. 但我无法弄清楚这一点。 How can I can solve this problem?
我怎样才能解决这个问题?
In checkout the order object doesn't exist until customer clicks on "Place Order", so you can't use the hook woocommerce_cancelled_order
located in cancel_order()
method. 在结帐时, 订单对象在客户点击“下订单”之前不存在,因此您无法使用位于
cancel_order()
方法中的hook woocommerce_cancelled_order
。
In checkout page you need to empty cart instead using a custom button and the following code will empty the cart and redirect to shop when "Cancel order" is clicked: 在结帐页面中,您需要使用自定义按钮清空购物车,以下代码将清空购物车并在点击“取消订单”时重定向到购物:
add_action( 'woocommerce_review_order_after_submit', 'checkout_reset_button', 10 );
function checkout_reset_button(){
echo '<br><br>
<a class="button alt" style="text-align:center;" href="?cancel=1">'.__("Cancel order", "woocommerce").'</a>';
}
add_action( 'template_redirect', 'checkout_reset_cart' );
function checkout_reset_cart() {
if( ! is_admin() && isset($_GET['cancel']) ) {
WC()->cart->empty_cart();
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works.
经过测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.