提问者:小点点

在特定条件下,我如何更改WooCommerce中结帐页面上的“您的订单”文本


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

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

共1个答案

匿名用户

要更改的文本位于签出/表单签出中。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 reviewhook,只有这些不适用于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 );