WooCommerce 添加到购物车后停止重定向

Stop the redirection after WooCommerce add to cart

我希望在用户点击添加到购物车按钮后完全删除任何重定向。

其实我没有使用产品页面。
我在产品中使用带有 link 的简单按钮,如下所示:?add-to-cart=492.

我的用户将点击我页面上的几个 "add to cart" 按钮,因此在点击第一个按钮后他无法被重定向到任何页面。

在页面的末尾,他会找到一个结账按钮,就是这样。

有什么实现方法吗?

谢谢

更新:

您的简单 html "add-to-cart" 按钮链接实际上是 href 值):

<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 short code [add-to-cart]:**

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

2) Html 代码 在您的页面文本编辑器中 - 为了防止重定向, href属性应该是:

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

This time your customers will not be redirected as before


结帐按钮

最后, 这是一个自定义短代码,它将输出 "Proceed to checkout" 按钮:

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 文件或任何插件文件。

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


原回答:

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

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

然后您可以使用以下方式添加自定义 "Proceed to checkout" 按钮:

  • 任何经典的 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
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

按钮 "Proceed to checkout" 将显示在该页面的底部。


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

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 文件或任何插件文件。

所有代码都经过测试并有效。

就像 woocommerce 本身在产品代码短代码中所做的那样,您可以只使用简单添加到购物车按钮的过滤器 'woocommerce_add_to_cart_form_action'。

// Change form action to avoid redirect.
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );