Example #1
0
 /**
  * Sync default attributes between product translations
  *
  * @param int       $post_id    Post ID
  * @param \WP_Post  $post       Post Object
  * @param boolean   $update     true if updating the post, false otherwise
  */
 public function sync_default_attributes($post_id, $post, $update)
 {
     // Don't sync if not in the admin backend nor on autosave
     if (!is_admin() && defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return;
     }
     // Don't sync if Default Attribute syncronization is disabled
     $metas = Meta::getProductMetaToCopy();
     if (!in_array('_default_attributes', $metas)) {
         return;
     }
     //  To avoid Polylang overwriting default attribute meta
     add_filter('delete_post_metadata', array($this, 'skip_default_attributes_meta'), 10, 4);
     add_filter('add_post_metadata', array($this, 'skip_default_attributes_meta'), 10, 4);
     add_filter('update_post_metadata', array($this, 'skip_default_attributes_meta'), 10, 4);
     // Don't sync if not a Variable Product
     $product = wc_get_product($post_id);
     if ($product && 'simple' === $product->product_type && Utilities::maybe_variable_product($product)) {
         // Maybe is Variable Product
         // New translations of Variable Products are first created as simple
         // Only need to sync for the new translation from source product
         // The other product translation stay untouched
         $attributes_translation = Utilities::get_default_attributes_translation($_GET['from_post'], $_GET['new_lang']);
         if (!empty($attributes_translation) && isset($attributes_translation[$_GET['new_lang']])) {
             update_post_meta($product->id, '_default_attributes', $attributes_translation[$_GET['new_lang']]);
         }
     } elseif ($product && 'variable' === $product->product_type) {
         // Variable Product
         // For each product translation, get the translated (default) terms/attributes
         $attributes_translation = Utilities::get_default_attributes_translation($product->id);
         $langs = pll_languages_list();
         foreach ($langs as $lang) {
             $translation_id = pll_get_post($product->id, $lang);
             if ($translation_id != $product->id) {
                 update_post_meta($translation_id, '_default_attributes', $attributes_translation[$lang]);
             }
         }
     }
 }