我该如何更改“您的订单"?在某些情况下,WooCommerce的结帐页面上的文本 [英] How can I change "Your order" text on checkout page in WooCommerce under certain conditions

查看:87
本文介绍了我该如何更改“您的订单"?在某些情况下,WooCommerce的结帐页面上的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我可以更改您的订单"结帐页面中的文字.但是我需要更改购物车中的特定产品还是购物车中的虚拟产品.

With this code I can change "Your Order" text in checkout page. But I need to change if specific product in my cart or virtual product is in my cart.

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

我找到了此代码,但在特定产品的其他位置.我该如何更改?

I found this code but for different place for specific product. how can I change it?

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

推荐答案

要更改的文本位于您将看到

  • woocommerce_checkout_before_order_review_heading
  • woocommerce_checkout_before_order_review钩子,只有这些钩子不适用于H3标记
  • woocommerce_checkout_before_order_review_heading and
  • woocommerce_checkout_before_order_review hooks, only these do not apply to the H3 tag

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

So gettext is recommended if you don't want to overwrite the template file.

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

To debug this and other text you can use

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


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


So to answer your question, this should suffice

  • 检查特定的产品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 );


  • 要检查您可以使用的虚拟产品

    • To check for virtual products you could use
    • 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 );
      

      这篇关于我该如何更改“您的订单"?在某些情况下,WooCommerce的结帐页面上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆