簡體   English   中英

Woocommerce將客戶重定向到上一頁

[英]Woocommerce Redirect Customers to Previous Page

我已經應用了包括this在內的所有可用代碼段,但是當客戶登錄時,它重定向到“我的帳戶頁面”,這很煩人。 如何解決呢?

為什么下面的代碼不起作用?

  add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
  function wc_custom_user_redirect( $redirect, $user ) {

      $role = $user->roles[0];

    if ( $role == 'customer' ) {
      //Redirect to the previous visited page or, if not available, to home
         $redirect = wp_get_referer() ? wp_get_referer() : home_url();
    }
    return $redirect;
  }

首先,在woocommerce_login_redirect過濾器處的wp_get_referer()將定位到帳戶頁面,您需要獲取引用者的引用者。 第二件事是它對我和其他許多人都不起作用,而$_SERVER['HTTP_REFERER']可以毫無問題地獲取引用。

這可以使用PHP會話來完成,但是它們會在Ngix和Varnish上通過服務器緩存導致問題。

可能的解決方案是:

傳遞隱藏的引薦來源網址字段,如下所示:

https://gist.github.com/EvanHerman/492c09fbb584e0c428ae

使用數據庫臨時存儲每個用戶ID的上一頁URL。 具有唯一ID的查詢字符串將添加到WooCommerce登錄表單的URL。 我在插件中使用以下解決方案:

https://wordpress.org/plugins/extra-settings-for-woocommerce/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM