如何在特定条件下更改 WooCommerce 结帐页面上的 "Your order" 文本

How can I change "Your order" text on checkout page in WooCommerce under certain conditions

使用此代码,我可以更改结帐页面中的“您的订单”文本。但如果我的购物车中的特定产品或虚拟产品在我的购物车中,我需要更改。

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;
}

您要更改的文本位于 checkout/form-checkout.php line 54

<h3 id="order_review_heading"><?php esc_html_e( 'Your order', 'woocommerce' ); ?></h3>

如您所见,在

之前和之后
  • woocommerce_checkout_before_order_review_heading
  • woocommerce_checkout_before_order_review 钩子,只有这些不适用于 H3 标签

因此,如果您不想覆盖模板文件,建议gettext

要调试此文本和其他文本,您可以使用

function filter_gettext( $translated, $text, $domain  ) {
    echo '<pre>', print_r( $text , 1 ), '</pre>';
    return $translated;
}
add_filter( 'gettext',  'filter_gettext', 10, 3 );

所以要回答你的问题,这就足够了

  • 检查特定产品 ID
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

  • 您可以使用以下代码检查多个产品 ID
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 );