WPML

Overview of the Issue

A REST request to the orders endpoint to retrieve details of an order can give unexpected results when the order was made in a secondary language, or if specifying a ‘lang’ parameter, and the order contains variable products.

The product_id of the parent product may be translated but the variation_id of the ordered variant may not, and the name of the product will be that of the parent and not that of the variant.

Workaround

This issue is fixed in WPML Multilingual & Multicurrency for WooCommerce 5.5.4. Updating solves it on your site, so you only need this workaround if you can’t update yet.

We have identified the underlying cause and will provide a fix in an upcoming release. In the meantime the issue can be fixed by editing the plugin file plugins/woocommerce-multilingual/classes/Rest/Wrapper/Orders/Languages.php and replacing the whole of the ‘prepare’ function (from around line 42) with

public function prepare( $response, $object, $request ) {
	$language = get_query_var( 'lang' ) ?: $request->get_param( 'lang' );
	$orderLanguage = WCML_Orders::getLanguage( $this->get_id( $object ) );

	if ( $orderLanguage !== $language ) { 
		$lineItems = Obj::propOr( [], 'line_items', $response->data );
		foreach ( $lineItems as $k => $item ) {
			$translatedProductId = wpml_object_id_filter( $item['product_id'], 'product', false, $language );
			$translatedVariationId = ( $item['variation_id'] == 0 ) ? 0 : wpml_object_id_filter( $item['variation_id'], 'product_variation', false, $language );

			if ( $translatedProductId ) {
				$response->data['line_items'][ $k ]['product_id'] = $translatedProductId;
				$translatedProduct = get_post( $translatedProductId );
				$postName = $translatedProduct->post_title;

				if ( $translatedVariationId ) {
					$response->data['line_items'][ $k ]['variation_id'] = $translatedVariationId;
					$translatedVariation = get_post( $translatedVariationId );
					$postName = $translatedVariation->post_title;
				}

				$response->data['line_items'][ $k ]['name'] = $postName;
			}
		}
	}

	return $response;
}

Still Seeing This Issue?

If updating doesn’t fix it, ask a support question and we’ll look into it with you.

Ask a support question

All known issues