Esempio n. 1
0
 function woocommerce_duplicate_product($new_id, $post)
 {
     global $sitepress, $wpdb;
     //duplicate original first
     $trid = $sitepress->get_element_trid($post->ID, 'post_' . $post->post_type);
     $orig_id = $sitepress->get_original_element_id_by_trid($trid);
     $orig_lang = $this->get_original_product_language($post->ID);
     $wc_admin = new WC_Admin_Duplicate_Product();
     if ($orig_id == $post->ID) {
         $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, false, $orig_lang);
         $new_trid = $sitepress->get_element_trid($new_id, 'post_' . $post->post_type);
         $new_orig_id = $new_id;
     } else {
         $post_to_duplicate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $orig_id));
         if (!empty($post_to_duplicate)) {
             $new_orig_id = $wc_admin->duplicate_product($post_to_duplicate);
             do_action('wcml_after_duplicate_product', $new_id, $post_to_duplicate);
             $sitepress->set_element_language_details($new_orig_id, 'post_' . $post->post_type, false, $orig_lang);
             $new_trid = $sitepress->get_element_trid($new_orig_id, 'post_' . $post->post_type);
             update_post_meta($new_id, '_icl_lang_duplicate_of', $new_orig_id);
             $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, $new_trid, $sitepress->get_current_language());
         }
     }
     $translations = $sitepress->get_element_translations($trid, 'post_' . $post->post_type);
     if ($translations) {
         foreach ($translations as $translation) {
             if (!$translation->original && $translation->element_id != $post->ID) {
                 $post_to_duplicate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $translation->element_id));
                 if (!empty($post_to_duplicate)) {
                     $new_id = $wc_admin->duplicate_product($post_to_duplicate);
                     $new_id_obj = get_post($new_id);
                     $new_slug = wp_unique_post_slug(sanitize_title($new_id_obj->post_title), $new_id, $post_to_duplicate->post_status, $post_to_duplicate->post_type, $new_id_obj->post_parent);
                     $wpdb->update($wpdb->posts, array('post_name' => $new_slug), array('ID' => $new_id));
                     do_action('wcml_after_duplicate_product', $new_id, $post_to_duplicate);
                     $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, $new_trid, $translation->language_code);
                     update_post_meta($new_id, '_icl_lang_duplicate_of', $new_orig_id);
                 }
             }
         }
     }
 }
 function woocommerce_duplicate_product($new_id, $post)
 {
     global $sitepress, $wpdb;
     $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, false, $sitepress->get_current_language());
     $new_trid = $sitepress->get_element_trid($new_id, 'post_' . $post->post_type);
     $trid = $sitepress->get_element_trid($post->ID, 'post_' . $post->post_type);
     $translations = $sitepress->get_element_translations($trid, 'post_' . $post->post_type);
     if ($translations) {
         $wc_admin = new WC_Admin_Duplicate_Product();
         foreach ($translations as $translation) {
             if ($translation->element_id != $post->ID) {
                 $post_to_duplicate = $wpdb->get_row("SELECT * FROM {$wpdb->posts} WHERE ID={$translation->element_id}");
                 if (!empty($post_to_duplicate)) {
                     $new_id = $wc_admin->duplicate_product($post_to_duplicate);
                     $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, $new_trid, $translation->language_code);
                 }
             }
         }
     }
 }
 function woocommerce_duplicate_product($new_id, $post)
 {
     global $sitepress, $wpdb;
     //duplicate original first
     $trid = $sitepress->get_element_trid($post->ID, 'post_' . $post->post_type);
     $orig_id = $sitepress->get_original_element_id_by_trid($trid);
     $orig_lang = $this->get_original_product_language($post->ID);
     $wc_admin = new WC_Admin_Duplicate_Product();
     if ($orig_id == $post->ID) {
         $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, false, $orig_lang);
         $new_trid = $sitepress->get_element_trid($new_id, 'post_' . $post->post_type);
     } else {
         $post_to_duplicate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $orig_id));
         if (!empty($post_to_duplicate)) {
             $new_orig_id = $wc_admin->duplicate_product($post_to_duplicate);
             do_action('wcml_after_duplicate_product', $new_id, $post_to_duplicate);
             $sitepress->set_element_language_details($new_orig_id, 'post_' . $post->post_type, false, $orig_lang);
             $new_trid = $sitepress->get_element_trid($new_orig_id, 'post_' . $post->post_type);
             $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, $new_trid, $sitepress->get_current_language());
         }
     }
     $translations = $sitepress->get_element_translations($trid, 'post_' . $post->post_type);
     if ($translations) {
         foreach ($translations as $translation) {
             if (!$translation->original && $translation->element_id != $post->ID) {
                 $post_to_duplicate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $translation->element_id));
                 if (!empty($post_to_duplicate)) {
                     $new_id = $wc_admin->duplicate_product($post_to_duplicate);
                     do_action('wcml_after_duplicate_product', $new_id, $post_to_duplicate);
                     $sitepress->set_element_language_details($new_id, 'post_' . $post->post_type, $new_trid, $translation->language_code);
                 }
             }
         }
     }
 }
 public function duplicate_product($new_id, $post)
 {
     if (class_exists('WC_Admin_Duplicate_Product')) {
         $dup = new WC_Admin_Duplicate_Product();
         if ($children_products = get_children('post_parent=' . $post->ID . '&post_type=' . TM_EPO_LOCAL_POST_TYPE)) {
             if ($children_products) {
                 $new_rules_ids = array();
                 foreach ($children_products as $child) {
                     $new_rules_ids[] = $dup->duplicate_product($this->get_product_to_duplicate($child->ID), $new_id, $child->post_status);
                 }
                 $new_rules_ids = array_filter($new_rules_ids);
                 if (!empty($new_rules_ids)) {
                     $children_products = get_children('post_parent=' . $post->ID . '&post_type=product_variation&order=ASC');
                     if ($children_products) {
                         $old_variations_ids = array();
                         foreach ($children_products as $child) {
                             $old_variations_ids[$child->menu_order] = $child->ID;
                         }
                         $old_variations_ids = array_filter($old_variations_ids);
                         $children_products = get_children('post_parent=' . $new_id . '&post_type=product_variation&order=ASC');
                         if ($children_products) {
                             $new_variations_ids = array();
                             foreach ($children_products as $child) {
                                 $new_variations_ids[$child->menu_order] = $child->ID;
                             }
                             $new_variations_ids = array_filter($new_variations_ids);
                             if (!empty($old_variations_ids) && !empty($new_variations_ids)) {
                                 foreach ($new_rules_ids as $rule_id) {
                                     $_regular_price = get_post_meta($rule_id, '_regular_price', true);
                                     /*
                                      * $key = attirbute
                                      * $k = variation
                                      * $v = price
                                      */
                                     $new_regular_price = array();
                                     if (is_array($_regular_price)) {
                                         foreach ($_regular_price as $key => $value) {
                                             if (is_array($value)) {
                                                 foreach ($value as $k => $v) {
                                                     if (!isset($new_regular_price[$key])) {
                                                         $new_regular_price[$key] = array();
                                                     }
                                                     $_new_key = array_search($k, $old_variations_ids);
                                                     if ($_new_key !== FALSE && $_new_key !== NULL) {
                                                         $_new_key = $new_variations_ids[$_new_key];
                                                     }
                                                     if ($_new_key !== FALSE && $_new_key !== NULL) {
                                                         $new_regular_price[$key][$_new_key] = $v;
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                     update_post_meta($rule_id, '_regular_price', $new_regular_price);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }