有了这段代码,我可以在结帐页面中更改“您的订单”文本。但如果购物车中有特定产品或虚拟产品,我需要更改。
function custom_wc_translations($translated){
$text = array(
'Your order' => 'Your new phrase',
'any other string' => 'New string',
);
$translated = str_ireplace( array_keys($text), $text, $translated );
return $translated;
}
add_filter( 'gettext', 'custom_wc_translations', 20 );
我找到了这个代码,但对于特定的产品,它适用于不同的地方。我怎样才能改变它?
add_filter( 'gettext', 'change_conditionally_order_review_heading_text', 10, 3 );
function change_conditionally_order_review_heading_text( $translated, $text, $domain ) {
if( $text === 'Your Order' && is_checkout() && ! is_wc_endpoint_url() ){
// HERE set the desired specific product ID
$targeted_product_id = 1122;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $targeted_product_id == $cart_item['data']->get_id() )
return __( 'İletişim Bilgileri', $domain );
}
}
return $translated;
}
要更改的文本位于签出/表单签出中。php
第54行
<h3 id="order_review_heading"><?php esc_html_e( 'Your order', 'woocommerce' ); ?></h3>
正如你会看到的,在这之前和之后
woocommerce\u checkout\u-before\u-order\u-review\u标题
和woocommerce\u checkout\u before\u order\u review
hook,只有这些不适用于H3
标记因此,如果不想覆盖模板文件,建议使用gettext
。
调试这个和其他可以使用的文本
function filter_gettext( $translated, $text, $domain ) {
echo '<pre>', print_r( $text , 1 ), '</pre>';
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );
回答你的问题这就足够了
function filter_gettext( $translated, $text, $domain ) {
if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {
// HERE set the desired specific product ID
$targeted_product_id = 1122;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $targeted_product_id == $cart_item['data']->get_id() ) {
$translated = __( 'İletişim Bilgileri', $domain );
}
}
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );
更新10/2020
function filter_gettext( $translated, $text, $domain ) {
if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {
// HERE set the desired specific product IDs
$targeted_product_ids = array( 1122, 30, 815 );
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// In array
if ( in_array( $cart_item['data']->get_id(), $targeted_product_ids ) ) {
$translated = __( 'İletişim Bilgileri', $domain );
}
}
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );
function filter_gettext( $translated, $text, $domain ) {
if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Is virtual
if ( $cart_item['data']->is_virtual() ) {
$translated = __( 'İletişim Bilgileri', $domain );
}
}
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );