/** * Function to create the duplicate */ function woocommerce_create_duplicate_from_product($post, $parent = 0) { 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; $suffix = ''; $post_status = 'publish'; } else { $post_parent = $post->post_parent; $post_status = 'draft'; $suffix = __(" (Copy)", 'woocommerce'); } $new_post_type = $post->post_type; $post_content = str_replace("'", "''", $post->post_content); $post_content_filtered = str_replace("'", "''", $post->post_content_filtered); $post_excerpt = str_replace("'", "''", $post->post_excerpt); $post_title = str_replace("'", "''", $post->post_title) . $suffix; $post_name = str_replace("'", "''", $post->post_name); $comment_status = str_replace("'", "''", $post->comment_status); $ping_status = str_replace("'", "''", $post->ping_status); // Insert the new template in the post table $wpdb->query("INSERT INTO {$wpdb->posts}\n\t\t\t(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)\n\t\t\tVALUES\n\t\t\t('{$new_post_author->ID}', '{$new_post_date}', '{$new_post_date_gmt}', '{$post_content}', '{$post_content_filtered}', '{$post_title}', '{$post_excerpt}', '{$post_status}', '{$new_post_type}', '{$comment_status}', '{$ping_status}', '{$post->post_password}', '{$post->to_ping}', '{$post->pinged}', '{$new_post_date}', '{$new_post_date_gmt}', '{$post_parent}', '{$post->menu_order}', '{$post->post_mime_type}')"); $new_post_id = $wpdb->insert_id; // Copy the taxonomies woocommerce_duplicate_post_taxonomies($post->ID, $new_post_id, $post->post_type); // Copy the meta information woocommerce_duplicate_post_meta($post->ID, $new_post_id); // Copy the children (variations) if ($children_products =& get_children('post_parent=' . $post->ID . '&post_type=product_variation')) { if ($children_products) { foreach ($children_products as $child) { woocommerce_create_duplicate_from_product(woocommerce_get_product_to_duplicate($child->ID), $new_post_id); } } } return $new_post_id; }
function duplicate_product($strtCnt, $dupCnt, $data, $msg, $count, $per, $perval) { $post_data = array(); for ($i = $strtCnt; $i < $dupCnt; $i++) { $post_id = $data[$i]; $post = get_post($post_id); if ($post->post_parent == 0) { $post_data[] = woocommerce_create_duplicate_from_product($post, 0, 'publish'); } else { $post_data[] = $data[$i]; } } $duplicate_count = count($post_data); if ($duplicate_count == $count) { $result = true; } else { $result = false; } if ($result == true) { $encoded['msg'] = $msg; $encoded['dupCnt'] = $dupCnt; $encoded['nxtreq'] = $_POST['part']; $encoded['per'] = $per; $encoded['val'] = $perval; } elseif ($result == false) { $encoded['msg'] = $activeModule . __('s were not duplicated', 'smart-manager'); } echo json_encode($encoded); exit; }
function sm_duplicate_product($strtCnt, $dupCnt, $data, $msg, $count, $per, $perval, &$woo_dup_obj) { $post_data = array(); for ($i = $strtCnt; $i < $dupCnt; $i++) { $post_id = $data[$i]; $post = get_post($post_id); if ($post->post_parent == 0) { if ($woo_dup_obj instanceof WC_Admin_Duplicate_Product) { $post_data[] = $woo_dup_obj->duplicate_product($post, 0, 'publish'); } else { $post_data[] = woocommerce_create_duplicate_from_product($post, 0, 'publish'); } } else { $post_data[] = $data[$i]; } } $duplicate_count = count($post_data); if ($duplicate_count == $count) { $result = true; } else { $result = false; } if ($result == true) { $encoded['msg'] = $msg; $encoded['dupCnt'] = $dupCnt; $encoded['nxtreq'] = $_POST['part']; $encoded['per'] = $per; $encoded['val'] = $perval; } elseif ($result == false) { $encoded['msg'] = $activeModule . __('s were not duplicated', $sm_text_domain); } echo json_encode($encoded); exit; }