WooCommerce添加到购物车后,停止重定向 [英] Stop the redirection after WooCommerce add to cart

查看:103
本文介绍了WooCommerce添加到购物车后,停止重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户单击添加到购物车按钮后,我希望完全删除所有重定向。

I wish to completely remove any redirection after a user click on the ADD TO CART button.

实际上我没有使用产品页面。

我正在使用一个带有产品链接的简单按钮,例如: ?add-to-cart = 492

Actually I am not using the products page.
I am using a simple button with the link to the product, like this: ?add-to-cart=492.

我的用户将单击我页面上的几个添加到购物车按钮,因此单击第一个按钮后,无法将他重定向到任何页面。

My user will click on several "add to cart" buttons on my page, so he cant be redirected to any page after clicking in the first button.

在页面末尾,他会发现要付款的CHECKOUT按钮,就是这样。

At the end of the page he will find a CHECKOUT button to pay and that is it.

任何想法如何实现呢?

谢谢

推荐答案

更新:

您的简单html添加到购物车按钮链接实际上就是这样的 href 值)

Your simple html "add-to-cart" button links are actually for example like that (the href value):

<a href="http://my-domain.com/site2/?add-to-cart=492" target="_self" class="button white is-larger carrinho">
    <span>ESCOLHER PACOTE</span>
</a>




因此它们每次都重定向到您的主页

So they are redirected each time to your home page






2解决方案:

1)使用 WooCommerce短代码[add-to-购物车] 这样:**

1) Use the WooCommerce short code [add-to-cart] this way:**


  • 无价格: [add_to_cart id = 492 show_price = false]

  • 价格: [add_to_cart id = 492 ]

  • Without price: [add_to_cart id="492" show_price="false"]
  • With the price: [add_to_cart id="492"]

2)页面文本中的 HTML代码编辑器-为防止重定向, href 属性应为:

2) Html code in your page text editor - To prevent redirections, the href attribute should be:

<a href="?add-to-cart=492" class="button white is-larger carrinho">
    <span>ESCOLHER PACOTE</span>
</a>




这次,您的客户不会像以前一样被重定向






结帐按钮

要完成,以下是一个自定义的简码,它将输出继续付款按钮:

To finish, here is a custom shortcode that will output the "Proceed to checkout" button:

if( !function_exists('proceed_to_checkout_button') ) {
    function proceed_to_checkout_button() {

        $checkout_url = wc_get_checkout_url();
        $button_txt = __('Proceed to checkout', 'woocommerce');

        $output = '<div class="wc-proceed-to-checkout">
            <a href="'. $checkout_url .'" class="checkout-button button alt wc-forward">
                '. $button_txt .'
            </a>
        </div>';

        return $output;
    }
    add_shortcode( 'checkout_button', 'proceed_to_checkout_button' );
}

代码包含在您活动的子主题的function.php文件中(


用法:只需将其添加到您的页面编辑器: [checkout_button]

Usage: just add this to your pages editor: [checkout_button]






原始答案:

首先,在WooCommerce设置中,您需要:

First, In WooCommerce settings you need to:


  • 启用**添加到购物车按钮上的Ajax(Woocommerce>设置>产品>显示)

  • 禁用添加到购物车按钮重定向(Woocommerce>设置>产品>显示)

  • Enable **Ajax on add-to-cart button (Woocommerce > Settings > Products > Display)
  • Disable the add-to-cart button redirection (Woocommerce > Settings > Products > Display)

然后您可以添加一个自定义继续付款按钮,使用:

Then you can add a custom "Proceed to checkout" button using:


  • 任何经典WordPress菜单(外观>菜单)

  • 在单个产品页面和产品档案上使用该自定义代码:

add_action('woocommerce_after_single_product', 'custom_checkout_button', 100);
add_action('woocommerce_after_shop_loop', 'custom_checkout_button', 100);
function custom_checkout_button() {

    $checkout_url = wc_get_checkout_url();
    $button_txt = __('Proceed to checkout', 'woocommerce');

    ?>
        <div class="wc-proceed-to-checkout">
            <a href="<?php echo $checkout_url; ?>" class="checkout-button button alt wc-forward">
                <?php echo $button_txt ?>
            </a>
        </div>
    <?php
}

代码起作用。您的活动子主题(或主题)或任何插件文件中的php文件。

继续结帐按钮将显示在底部

The button "Proceed to checkout" will be show at the bottom of this pages.

如果您想跳过购物车页面:

If you want to skip the cart page:

add_action('template_redirect', 'skip_cart_page_redirecting_to_checkout');
function skip_cart_page_redirecting_to_checkout() {

    // If is cart page and cart is not empty
    if( is_cart() && ! WC()->cart->is_empty() )
        wp_redirect( wc_get_checkout_url() );
}

代码包含在您活动的子主题的function.php文件中(

所有代码都经过测试并且可以正常工作。

All code is tested and works.

这篇关于WooCommerce添加到购物车后,停止重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆