This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Shekhar Bhandari 5 months, 1 week ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
May 14, 2019 at 4:23 pm #3809121

severinP

The filter

add_filter( 'woocommerce_cart_item_name', array( $this, 'translated_cart_item_name' ), -1, 2);
/**
 * @param string $title
 * @param array  $values
 *
 * @param        $key
 *
 * @return string
 */
function translated_cart_item_name( $title, array $values, $key ) {

    if ( $values ) {

        $product_id = $values['variation_id'] ? $values['variation_id'] : $values['product_id'];

        $translated_product_id = apply_filters( 'translate_object_id', $product_id, 'product', true );
        $translated_title      = get_the_title( $translated_product_id );
        $product = $values['data'];
        $permalink = apply_filters( 'woocommerce_cart_item_permalink', $product->is_visible() ? $product->get_permalink( $values ) : '', $values, $key );


        if ( strstr( $title, '</a>' ) ) {
            $title = sprintf( '<a href="%s">%s</a>', $permalink, $translated_title );
        } else {
            $title = $translated_title . ' ';
        }
    }

    return $title;
}

defined in WCML_WC_Strings is overwriting any custom permalink or html in the product name

This filter is not needed, woocommerce provides 2 other filters to translate the product in the cart instead of trying to translate pre processed HTML

The 2 filters are 'woocommerce_cart_item_product_id' and 'woocommerce_cart_item_product'

add_filter( 'woocommerce_cart_item_product_id', function( $id ) {
    return apply_filters( 'wpml_object_id', $id, 'product', true );
});

add_filter( 'woocommerce_cart_item_product', function( $product ) {
    $id = $product->get_id();
    if ($id != ($tr_id = apply_filters( 'wpml_object_id', $id, 'product', true ) ) ) {
        return wc_get_product( $tr_id );
    }
    return $product;
});

Please remove the old filter and implement the other two

Thanks

May 15, 2019 at 4:54 am #3812275

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Welcome to WPML support. I'd be assisting you further on this issue.

You mentioned "is overwriting any custom permalink or HTML in the product name" could you please provide an example for this so I can reproduce this issue on my fresh install and report it to developers?

Thank you very much for your suggestion. 🙂

Look forward to your reply.

Thanks

The topic ‘[Closed] Filter to translate woocommerce_cart_item_name overwrites filtered permalink’ is closed to new replies.