WooCommerce 即將包含的一個非常常見的功能 – 同意我的意見?同時,我們可以簡單地新增一些程式碼到我們的 functions.php 並將買方重定向到感謝頁面。可能,此感謝頁面將具有特定的加售,社交媒體分享和其他可幫助您轉換額外銷售的資訊。所以,這是我們能做的。

我們的 WooCommerce 問題:將新訂單重定向到自定義謝謝頁面
PHP Snippet 1(更快的重定向):重定向到自定義謝謝頁面
/** * @snippet WooCommerce: Redirect to Custom Thank you Page * @sourcecode https://businessbloomer.com/?p=490 * @author Rodolfo Melogli * @testedwith WooCommerce 2.5.5 */ // Redirect custom thank you add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom'); function bbloomer_redirectcustom( $order_id ){ $order = new WC_Order( $order_id ); $url = 'http://yoursite.com/custom-url'; if ( $order->status != 'failed' ) { wp_redirect($url); exit; } }
PHP Snippet 2(帶 JS):重定向到自定義謝謝頁面
/** * @snippet WooCommerce: Redirect to Custom Thank you Page * @sourcecode https://businessbloomer.com/?p=490 * @author Rodolfo Melogli * @compatible WooCommerce 2.4.7 */ // Redirect custom thank you add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom'); function bbloomer_redirectcustom( $order_id ){ $order = new WC_Order( $order_id ); $url = 'http://yoursite.com/custom-url'; if ( $order->status != 'failed' ) { echo "<script type='text/javascript'>window.location = '" . $url . "'</script>"; } }
「重定向對我來說不行嗎?」
我最近在一個客戶的網站上工作,並使用了上面的兩個片段 – 沒有任何工作。我甚至嘗試使用重定向外掛 (簡單 301 重定向),但是也沒有。
這是我發現的:在編輯 WooCommerce 端點設定時,他在 「訂單」 和 「已收到」 之間留下了一個空白 (見下圖,Google 將丹麥文字翻譯成英文) 。
就是這樣
如何新增此程式碼?
1 、您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部 (如果是 CSS 程式碼,請新增到子主題的 style.css 檔案底部) 修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。
2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 程式碼也可開啟網站前臺編輯器的 【自定義】,複製程式碼新增到自定義 css 中。
此程式碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。