musa baltaci
musa baltaci

Reputation: 139

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

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

Upvotes: 2

Views: 3277

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

The text you want to change is located in checkout/form-checkout.php line 54

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

As you will see there are just before and after the

  • woocommerce_checkout_before_order_review_heading and
  • woocommerce_checkout_before_order_review hooks, only these do not apply to the H3 tag

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

  • Check for a specific product 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 );

UPDATE 10/2020

  • You can use the following code to check for multiple product IDs
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 );

Upvotes: 4

Related Questions