1

I am building a little plugin in Woocommerce to add some custom fields. It's very simple:

function save_option_fields($post_id) {
  update_post_meta($post_id, '_energy', $_POST['_energy']);
  update_post_meta($post_id, '_fat', $_POST['_fat']);
}
add_action( 'woocommerce_process_product_meta_simple', 'save_option_fields');

The problem is, since this metadata are only numbers, they don't need to be translated and I want to share them with all other languages on the site.

Right now, it works fine, but data is just saved for one language (or one post).

The site is using polylang plugin. I don't know if there's a WP/WC functions, or even from Polylabg itself, to address this.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
pzin
  • 4,200
  • 2
  • 28
  • 49
  • You may try to get it from the original post and print it on all other languages. – mujuonly May 03 '20 at 19:43
  • @mujuonly thanks for your suggestion, but that would not work well in the form logic of the product. For example, price is not greyed out but in the main language. I would like to find the right solution instead of a workaround. – pzin May 04 '20 at 10:03

1 Answers1

0

I'm not a polylang user, but If you are trying to save the metadata in all the languages, an idea could be something like this:

Eg (untested):

function save_option_fields($post_id) {

  update_post_meta($post_id, '_energy', $_POST['_energy']);
  update_post_meta($post_id, '_fat', $_POST['_fat']);

  $languages = array('en','es'); // or use a polylang function to get the site languages in use as an array

  foreach( $languages as $language ) {

    $translated_id = pll_get_post( $post_id, $language ); // this should return the translated id, according to polylang docs

    update_post_meta($translated_id, '_energy', $_POST['_energy']);
    update_post_meta($translated_id, '_fat', $_POST['_fat']);

  }

}
add_action( 'woocommerce_process_product_meta_simple', 'save_option_fields');

Thoughts? :)

FrancescoCarlucci
  • 1,570
  • 12
  • 13