简体   繁体   English

在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面

[英]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.

相关问题 更改商店页面中 woocommerce 商店中“添加到购物车”按钮的重定向 - Changing the redirect of the “add to cart” button in woocommerce store in Shop page 将添加到购物车按钮重定向到 WooCommerce 中 1 个特定登录页面的结帐页面 - Redirect add to cart button to checkout page for 1 specific landing page in WooCommerce 如果购物车为空,购物车页面将重定向到 WooCommerce 中的商店页面? - If cart is empty, the cart page will redirect to shop page in WooCommerce? 仅在商店中使用 woocommerce 中的贝宝结帐替换添加到购物车按钮 - replacing add to cart button with paypal checkout in woocommerce only on shop Woocommerce添加到购物车按钮重定向到结帐 - Woocommerce add to cart button redirect to checkout Woocommerce - 将商店页面上的“添加到购物车”重定向到产品页面? - Woocommerce - Redirect Add-to-Cart on Shop page to Product Page? WooCommerce:更改空购物车页面中“返回商店”按钮的标签 - WooCommerce: Change the label of the “Return to shop” button in the empty cart page 从“清空购物车”页面移除或隐藏 WooCommerce“返回商店”按钮? - Removing or hiding WooCommerce "Return to Shop" button from 'empty cart' page? Woocommerce商店页面(类别页面模板)添加到购物车按钮行为 - Woocommerce shop page (category page template) Add to cart button behavior 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM