WooCommerce 添加到购物车重定向到上一个 URL [英] WooCommerce Add to cart redirection to previous URL

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

问题描述

应用户的要求,我需要单个产品页面上的添加到购物车按钮,以便在单击将产品添加到购物车后将用户重定向到上一页.

At the request of users, I need the add to cart button on the individual product page to redirect users to the previous page after clicking add product to cart.

使用以下代码,客户返回到特定页面(在本例中为商店页面):

With following code, customer returns to a specific page (in this case the shop page):

function my_custom_add_to_cart_redirect( $url ) {

    $url = get_permalink( 311 ); // URL to redirect to (1 is the page ID here)

    return $url;

}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

使用此代码,用户可以通过页面 ID 返回到特定页面.在这种情况下,它是商店页面

with this code the users return to a specific page by means of the page id. in this case it is the store page

我希望用户被重定向到上一页以查看产品,任何可以帮助我的想法.

I would like users to be redirected to the previous page to see the product, any idea that can help me.

谢谢!

推荐答案

2021 更新

以下内容将向 WC Session 保存一个简短的 Url 历史记录,允许在添加到购物车时将客户重定向到以前的 URL:

The following will save to WC Session a short Url history that Will allow, on add to cart, to redirect customer to previous URL:

// Early enable customer WC_Session
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
    if ( is_user_logged_in() || is_admin() )
        return;

    if ( isset(WC()->session) && ! WC()->session->has_session() ) {
        WC()->session->set_customer_session_cookie( true );
    }
}

// Set previous URL history in WC Session
add_action( 'template_redirect', 'wc_request_history' );
function wc_request_history() {
    // Get from WC Session the request history
    $history = (array) WC()->session->get('request_history');

    // Keep only 2 request Urls in the array
    if( count($history) > 1){
        $removed = array_shift($history);
    }

    $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

    if( ! in_array($current_url, $history) ) {
        // Set current url request in the array
        $history[] = $current_url;
    }

    // Save to WC Session the updated request history
    WC()->session->set('request_history', $history);
}

// The add to cart redirect to previous URL
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_redirect_to_previous_url' );
function add_to_cart_redirect_to_previous_url( $redirect_url ) {
    // Get from WC Session the request history
    $history = (array) WC()->session->get('request_history');

    if ( count($history) == 2 ) {
        $redirect_url = reset($history);
    } else {
        // Other custom redirection (optional)
    }

    return $redirect_url;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

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

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