2

I have a website using WooCommerce in the Slovenian language that is being translated with WPML into multiple languages and English will become the default one. Products were all set in Slovenian language and use a custom field (ACF) for an image. The products are translated but the images are duplicated which causes issues in one case. The image should be of the original product.

I have now fixed this by using this:

$preview_img_id = get_field( 'preview_image', apply_filters( 'wpml_object_id', get_the_ID(), 'product', FALSE, 'sl' ) );

But this is not a good solution in the long term because new products might be added in another language first.

What function can I use to get the ID of the original post/product?

jstneti
  • 21
  • 6
  • Just set the image field to "copy once" instead of "copy" - then you can just change it in the translation if needed. So the original image is being copied over, but you can change it. – Stender Nov 01 '21 at 10:29

1 Answers1

0

I'm not sure I understand what "causes issues in one case", but I believe you can omit the $ulanguage_code and set $return_original_if_missing to TRUE. Documentation:

$ulanguage_code - (mixed) (Optional) If missing or NULL, it will use the current language. If set to a language code, it will return a translation for that language code or the original if the translation is missing and $return_original_if_missing is set to TRUE. Default is NULL

If you always want to get the ID of the original product, then maybe you could trick it into doing this by specifying a non-existent language code, for example:

apply_filters( 'wpml_object_id', get_the_ID(), 'product', TRUE, 'xx' )
montrealist
  • 5,593
  • 12
  • 46
  • 68