在WooCommerce结账页面上更改“下订单”按钮的文本

如果你相信一件事,就去相信,不用等别人来告诉你行或者不行!早安!你不能改变过去,但你可以改变未来。早安!争气永远比生气漂亮。

有时您可能想更改标准的结账按钮文本,例如“下订单”,“继续进行PayPal”等。在本好代码教程中,我将向您展示几种方法。

请注意,此文字会根据所选的付款方式动态更改:

如您所见,我已经将文本更改为“提交”

从理论上讲,如果您了解WooCommerce模板结构,则可以替换此文件中的按钮文本:

但请不要这样做,因为通过钩子可以实现的。我会展示给您看。

方法1:使用woocommerce_order_button_text钩子更改文本

最简单的方法是,将这段代码复制到当前主题的functions.php文件中(最好是添加到子主题或自定义插件,否则,每次主题更新后,您所做的更改都会丢失)。

/**
 * @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'; // 修改这里的文字即可
}

如果购物车中有特定产品,或者购物车中有特定类别的产品,您还可以更改按钮文本。

add_filter( 'woocommerce_order_button_text', 'misha_custom_button_text_for_product' );
 
function misha_custom_button_text_for_product( $button_text ) {
 
	$product_id = 18; // 可以修改这里的产品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钩子更改文本

使用此函数,我们主要是在woocommerce_order_button_html过滤器挂钩中使用PHP函数 str_replace() 进行文本替换。这是实现方法:

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仅接受一个参数,即按钮HTML。该挂钩还允许您从头开始创建按钮的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>';
}

为支付网关更改按钮文本

首先,您的自定义支付网关可能没有这方面的钩子,其次,当然,您不能直接在支付网关插件文件中进行更改。

那么该怎么办?JavaScript?当然不!

即使支付网关没有这个钩子,它也必须支持本地化。因此,在这种情况下, 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' ) { // 检测旧文本
		$translated_text = 'Pay with PayPal'; // 在这里设置新的按钮文本
	}
 
	return $translated_text;
}

您可以将此代码应用于任何支付网关,只需检测旧的按钮文本,然后在第10行代码中替换为新的文本即可。

请小心,因为您要覆盖的字符串(例如“ Proceed to PayPal”)可能会在您网站的其他地方使用。如果是这样,只需添加一个条件判断。

声明:原文出自 https://rudrastyh.com/woocommerce/place-order-button-text.html ,由 WordPress大学 翻译整理,转载请保留本声明!

以上就是在WooCommerce结账页面上更改“下订单”按钮的文本。别老想着取悦别人,你越在乎别人,就越卑微。只有提升自己,取悦自己,并让别人来取悦你,才会令你更有价值。一辈子不长,记住:对自己好点。早安!更多关于在WooCommerce结账页面上更改“下订单”按钮的文本请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
WooCommerce 检查用户是否有未完成支付的订单

WooCommerce自定义感谢页面的内容

WordPress和WooCommerce内置的Cookies大全

如何禁用WooCommerce 4.0+新增的仪表盘和分析页面

WooCommerce重新排序结账字段