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;
}