有時(shí)您可能想更改標(biāo)準(zhǔn)的結(jié)賬按鈕文本,例如“下訂單”,“繼續(xù)進(jìn)行PayPal”等。在本教程中,我將向您展示幾種方法。
請(qǐng)注意,此文字會(huì)根據(jù)所選的付款方式動(dòng)態(tài)更改:

從理論上講,如果您了解WooCommerce模板結(jié)構(gòu),則可以替換此文件中的按鈕文本:

但請(qǐng)不要這樣做,因?yàn)橥ㄟ^(guò)鉤子可以實(shí)現(xiàn)的。我會(huì)展示給您看。
方法1:使用woocommerce_order_button_text鉤子更改文本
最簡(jiǎn)單的方法是,將這段代碼復(fù)制到當(dāng)前主題的functions.php文件中(最好是添加到子主題或自定義插件,否則,每次主題更新后,您所做的更改都會(huì)丟失)。
/**
* @snippet 更改“下訂單”按鈕文本 @ WooCommerce Checkout
* @sourcecode https://rudrastyh.com/?p=8327#woocommerce_order_button_text
* @author Misha Rudrastyh
*/
add_filter( 'woocommerce_order_button_text', 'misha_custom_button_text' );
function misha_custom_button_text( $button_text ) {
return 'Submit'; // 修改這里的文字即可
}
如果購(gòu)物車(chē)中有特定產(chǎn)品,或者購(gòu)物車(chē)中有特定類(lèi)別的產(chǎn)品,您還可以更改按鈕文本。
add_filter( 'woocommerce_order_button_text', 'misha_custom_button_text_for_product' );
function misha_custom_button_text_for_product( $button_text ) {
$product_id = 18; // 可以修改這里的產(chǎn)品ID
if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product_id ) ) ) {
$button_text = 'Submit';
}
return $button_text;
}
方法2:使用woocommerce_order_button_html鉤子更改文本
使用此函數(shù),我們主要是在woocommerce_order_button_html過(guò)濾器掛鉤中使用PHP函數(shù) str_replace() 進(jìn)行文本替換。這是實(shí)現(xiàn)方法:
add_filter( 'woocommerce_order_button_html', 'misha_custom_button_html' );
function misha_custom_button_html( $button_html ) {
$button_html = str_replace( 'Place order', 'Submit', $button_html );
return $button_html;
}
該掛鉤woocommerce_order_button_html僅接受一個(gè)參數(shù),即按鈕HTML。該掛鉤還允許您從頭開(kāi)始創(chuàng)建按鈕的HTML。
add_filter( 'woocommerce_order_button_html', 'misha_custom_button_html' );
function misha_custom_button_html( $button_html ) {
$order_button_text = 'Submit';
$button_html = '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>';
}
為支付網(wǎng)關(guān)更改按鈕文本
首先,您的自定義支付網(wǎng)關(guān)可能沒(méi)有這方面的鉤子,其次,當(dāng)然,您不能直接在支付網(wǎng)關(guān)插件文件中進(jìn)行更改。
那么該怎么辦?JavaScript?當(dāng)然不!
即使支付網(wǎng)關(guān)沒(méi)有這個(gè)鉤子,它也必須支持本地化。因此,在這種情況下, gettext 鉤子將為我們提供幫助。
/**
* @snippet Change "Proceed to PayPal" Button text @ WooCommerce Checkout
* @sourcecode https://rudrastyh.com/?p=8327#payment_gateways_text
* @author Misha Rudrastyh
*/
add_filter( 'gettext', 'misha_custom_paypal_button_text', 20, 3 );
function misha_custom_paypal_button_text( $translated_text, $text, $domain ) {
if( $translated_text == 'Proceed to PayPal' ) { // 檢測(cè)舊文本
$translated_text = 'Pay with PayPal'; // 在這里設(shè)置新的按鈕文本
}
return $translated_text;
}
您可以將此代碼應(yīng)用于任何支付網(wǎng)關(guān),只需檢測(cè)舊的按鈕文本,然后在第10行代碼中替換為新的文本即可。

請(qǐng)小心,因?yàn)槟采w的字符串(例如“ Proceed to PayPal”)可能會(huì)在您網(wǎng)站的其他地方使用。如果是這樣,只需添加一個(gè)條件判斷。
聲明:原文出自 https://rudrastyh.com/woocommerce/place-order-button-text.html ,由 WordPress大學(xué) 翻譯整理,轉(zhuǎn)載請(qǐng)保留本聲明!




