WooCommerce 清空購物車返回訊息+網址修改

WooCommerce 清空購物車返回訊息+網址修改

隨手筆記,我覺得我不知道民國幾年還會有機會用到這串修改語法。

這次的案例是客戶想修改清空購物車後,會有一顆按鈕「返回商店」網址是 /shop/

我個人是覺得客戶客製很多小地方其實不太必要,但遇到了就是得修改 🙁

WooCommerce 清空購物車返回訊息+網址修改
上圖是已經修改完成的樣子,心好累。

所幸修改很簡單,兩段語法可以解決這個部分,主要是利用「woocommerce_return_to_shop_redirect」與「woocommerce_return_to_shop_text」就可以處理了。

語法如下:

//修改返回SHOP網址
add_filter( 'woocommerce_return_to_shop_redirect', 'uni_woo_return_to_shop_button_modify' );
function uni_woo_return_to_shop_button_modify() {
    $url = 'https://www.your-url.com'; 
    return $url;
}

//修改按鈕文字
add_filter('woocommerce_return_to_shop_text', 'uni_woo_shop_button_text_modify');
function uni_woo_shop_button_text_modify() {
    $shop_button_text = "回到首頁"; 
    return $shop_button_text;
}

woocommerce_return_to_shop_redirect 負責處理網址的修改,預設是「/shop」商品頁面,我們直接改網址就可以了。

Wordfence 干擾影響登入錯誤題示訊息翻譯

Wordfence 干擾影響登入錯誤題示訊息翻譯

對,你沒有看錯,我也沒有寫錯,我今天要說的是「Wordfence」這套安全防火牆外掛影響到了 WooCommerce 的登入錯誤提示訊息,預設 WC 示有翻譯的,但不知道為什麼安裝了 Wordfence 後會影響到該字串的翻譯,都會變強制英文的。

Wordfence 干擾影響登入錯誤題示訊息翻譯
就很莫名其妙,你即使找遍了 WC 的翻譯都是正確無誤的

今天我們只要用 Code Snippets 插入以下程式碼就好了

function uni_wc_login_error_translation( $translated, $original, $domain ) {
    
    switch ($original) {
        case '<strong>ERROR</strong>: The username or password you entered is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?':
            $translated = '<strong>錯誤</strong>: 使用者的帳號或者密碼錯誤 <a href="%2$s" title="忘記密碼?">忘記密碼</a>?';
            break;
    }
    
    return $translated;
}

add_filter( 'gettext', 'uni_wc_login_error_translation', 10, 3 );

以上很好理解,就直接抓取那串字串並且翻譯替代:

case ‘<strong>ERROR</strong>: The username or password you entered is incorrect. <a href=”%2$s” title=”Password Lost and Found”>Lost your password</a>?‘:

紅字就是要抓取的字串,今天我們的兇手就是這一串的語法,調整一下

$translated = 後面的就是要翻譯的文字填上

Wordfence 干擾影響登入錯誤題示訊息翻譯

是不是很簡單呢?