Example #1
0
 /**
  * Translate Variation for given variable product
  *
  * @param integer  $ID     product variable ID
  * @param \WP_Post $post   Product Post
  * @param boolean  $update true if update , false otherwise
  *
  * @return boolean
  */
 public function duplicateVariations($ID, \WP_Post $post, $update)
 {
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return;
     }
     global $pagenow;
     if (!in_array($pagenow, array('post.php', 'post-new.php'))) {
         return;
     }
     $product = wc_get_product($ID);
     if (!$product) {
         return;
     }
     $from = null;
     if (pll_get_post_language($ID) == pll_default_language()) {
         $from = $product;
     } else {
         if (isset($_GET['from_post'])) {
             /*
              * This check will make sure that variation , will be
              * created for brand new products which are not saved yet by user
              */
             $from = Utilities::getProductTranslationByID(esc_attr($_GET['from_post']), pll_default_language());
         } else {
             $from = Utilities::getProductTranslationByObject($product, pll_default_language());
         }
     }
     if (!$from instanceof \WC_Product_Variable) {
         return;
     }
     $langs = pll_languages_list();
     foreach ($langs as $lang) {
         $variation = new Variation($from, Utilities::getProductTranslationByObject($product, $lang));
         remove_action('save_post', array($this, __FUNCTION__), 10);
         $variation->duplicate();
         add_action('save_post', array($this, __FUNCTION__), 10, 3);
     }
 }