WC_Admin_Duplicate_Product::duplicate_product PHP Méthode

duplicate_product() public méthode

Function to create the duplicate of the product.
public duplicate_product ( mixed $post, integer $parent, string $post_status = '' ) : integer
$post mixed
$parent integer (default: 0)
$post_status string (default: '')
Résultat integer
        public function duplicate_product($post, $parent = 0, $post_status = '')
        {
            global $wpdb;
            $new_post_author = wp_get_current_user();
            $new_post_date = current_time('mysql');
            $new_post_date_gmt = get_gmt_from_date($new_post_date);
            if ($parent > 0) {
                $post_parent = $parent;
                $post_status = $post_status ? $post_status : 'publish';
                $suffix = '';
                $post_title = $post->post_title;
            } else {
                $post_parent = $post->post_parent;
                $post_status = $post_status ? $post_status : 'draft';
                $suffix = ' ' . __('(Copy)', 'woocommerce');
                $post_title = $post->post_title . $suffix;
            }
            // Insert the new template in the post table
            $wpdb->insert($wpdb->posts, array('post_author' => $new_post_author->ID, 'post_date' => $new_post_date, 'post_date_gmt' => $new_post_date_gmt, 'post_content' => $post->post_content, 'post_content_filtered' => $post->post_content_filtered, 'post_title' => $post_title, 'post_excerpt' => $post->post_excerpt, 'post_status' => $post_status, 'post_type' => $post->post_type, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_password' => $post->post_password, 'to_ping' => $post->to_ping, 'pinged' => $post->pinged, 'post_modified' => $new_post_date, 'post_modified_gmt' => $new_post_date_gmt, 'post_parent' => $post_parent, 'menu_order' => $post->menu_order, 'post_mime_type' => $post->post_mime_type));
            $new_post_id = $wpdb->insert_id;
            // Set title for variations
            if ('product_variation' === $post->post_type) {
                /* translators: 1: variation id 2: product name */
                $post_title = sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), absint($new_post_id), esc_html(get_the_title($post_parent)));
                $wpdb->update($wpdb->posts, array('post_title' => $post_title), array('ID' => $new_post_id));
            }
            // Set name and GUID
            if (!in_array($post_status, array('draft', 'pending', 'auto-draft'))) {
                $wpdb->update($wpdb->posts, array('post_name' => wp_unique_post_slug(sanitize_title($post_title, $new_post_id), $new_post_id, $post_status, $post->post_type, $post_parent), 'guid' => get_permalink($new_post_id)), array('ID' => $new_post_id));
            }
            // Copy the taxonomies
            $this->duplicate_post_taxonomies($post->ID, $new_post_id, $post->post_type);
            // Copy the meta information
            $this->duplicate_post_meta($post->ID, $new_post_id);
            // Copy the children (variations)
            $exclude = apply_filters('woocommerce_duplicate_product_exclude_children', false);
            if (!$exclude && ($children_products = get_children('post_parent=' . $post->ID . '&post_type=product_variation'))) {
                foreach ($children_products as $child) {
                    $this->duplicate_product($this->get_product_to_duplicate($child->ID), $new_post_id, $child->post_status);
                }
            }
            // Clear cache
            clean_post_cache($new_post_id);
            return $new_post_id;
        }

Usage Example

 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);
                 }
             }
         }
     }
 }
All Usage Examples Of WC_Admin_Duplicate_Product::duplicate_product