Fixed duplicate product functionality
This commit is contained in:
parent
70a799757c
commit
f8b3b65ada
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
/**
|
||||
* Functions used to duplicate a product
|
||||
*
|
||||
* Based on 'Duplicate Post' (http://www.lopo.it/duplicate-post-plugin/) by Enrico Battocchi
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category Admin Includes
|
||||
* @package WooCommerce
|
||||
*/
|
||||
|
||||
function woocommerce_duplicate_product() {
|
||||
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_post_save_as_new_page' == $_REQUEST['action'] ) ) ) {
|
||||
wp_die(__('No product to duplicate has been supplied!', 'woocommerce'));
|
||||
}
|
||||
|
||||
// Get the original page
|
||||
$id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
|
||||
check_admin_referer( 'woocommerce-duplicate-product_' . $id );
|
||||
$post = woocommerce_get_product_to_duplicate($id);
|
||||
|
||||
// Copy the page and insert it
|
||||
if (isset($post) && $post!=null) {
|
||||
$new_id = woocommerce_create_duplicate_from_product($post);
|
||||
|
||||
// If you have written a plugin which uses non-WP database tables to save
|
||||
// information about a page you can hook this action to dupe that data.
|
||||
do_action( 'woocommerce_duplicate_product', $new_id, $post );
|
||||
|
||||
// Redirect to the edit screen for the new draft page
|
||||
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_id ) );
|
||||
exit;
|
||||
} else {
|
||||
wp_die(__('Product creation failed, could not find original product:', 'woocommerce') . ' ' . $id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a product from the database
|
||||
*/
|
||||
function woocommerce_get_product_to_duplicate($id) {
|
||||
global $wpdb;
|
||||
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
|
||||
if (isset($post->post_type) && $post->post_type == "revision"){
|
||||
$id = $post->post_parent;
|
||||
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
|
||||
}
|
||||
return $post[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
endif;
|
||||
|
||||
$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
|
||||
(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)
|
||||
VALUES
|
||||
('$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);
|
||||
|
||||
endforeach;
|
||||
|
||||
endif;
|
||||
|
||||
return $new_post_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the taxonomies of a post to another post
|
||||
*/
|
||||
function woocommerce_duplicate_post_taxonomies($id, $new_id, $post_type) {
|
||||
global $wpdb;
|
||||
$taxonomies = get_object_taxonomies($post_type); //array("category", "post_tag");
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
$post_terms = wp_get_object_terms($id, $taxonomy);
|
||||
for ($i=0; $i<count($post_terms); $i++) {
|
||||
wp_set_object_terms($new_id, $post_terms[$i]->slug, $taxonomy, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the meta information of a post to another post
|
||||
*/
|
||||
function woocommerce_duplicate_post_meta($id, $new_id) {
|
||||
global $wpdb;
|
||||
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id");
|
||||
|
||||
if (count($post_meta_infos)!=0) {
|
||||
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
|
||||
foreach ($post_meta_infos as $meta_info) {
|
||||
$meta_key = $meta_info->meta_key;
|
||||
$meta_value = addslashes($meta_info->meta_value);
|
||||
$sql_query_sel[]= "SELECT $new_id, '$meta_key', '$meta_value'";
|
||||
}
|
||||
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
|
||||
$wpdb->query($sql_query);
|
||||
}
|
||||
}
|
|
@ -7,40 +7,6 @@
|
|||
* @package WooCommerce
|
||||
*/
|
||||
|
||||
/**
|
||||
* Duplicate a product action
|
||||
*
|
||||
* Based on 'Duplicate Post' (http://www.lopo.it/duplicate-post-plugin/) by Enrico Battocchi
|
||||
*/
|
||||
add_action('admin_action_duplicate_product', 'woocommerce_duplicate_product_action');
|
||||
|
||||
function woocommerce_duplicate_product_action() {
|
||||
|
||||
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_post_save_as_new_page' == $_REQUEST['action'] ) ) ) {
|
||||
wp_die(__('No product to duplicate has been supplied!', 'woocommerce'));
|
||||
}
|
||||
|
||||
// Get the original page
|
||||
$id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
|
||||
check_admin_referer( 'woocommerce-duplicate-product_' . $id );
|
||||
$post = woocommerce_get_product_to_duplicate($id);
|
||||
|
||||
// Copy the page and insert it
|
||||
if (isset($post) && $post!=null) {
|
||||
$new_id = woocommerce_create_duplicate_from_product($post);
|
||||
|
||||
// If you have written a plugin which uses non-WP database tables to save
|
||||
// information about a page you can hook this action to dupe that data.
|
||||
do_action( 'woocommerce_duplicate_product', $new_id, $post );
|
||||
|
||||
// Redirect to the edit screen for the new draft page
|
||||
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_id ) );
|
||||
exit;
|
||||
} else {
|
||||
wp_die(__('Product creation failed, could not find original product:', 'woocommerce') . ' ' . $id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate a product link on products list
|
||||
*/
|
||||
|
@ -85,110 +51,6 @@ function woocommerce_duplicate_product_post_button() {
|
|||
endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a product from the database
|
||||
*/
|
||||
function woocommerce_get_product_to_duplicate($id) {
|
||||
global $wpdb;
|
||||
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
|
||||
if ($post->post_type == "revision"){
|
||||
$id = $post->post_parent;
|
||||
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
|
||||
}
|
||||
return $post[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
endif;
|
||||
|
||||
$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
|
||||
(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)
|
||||
VALUES
|
||||
('$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);
|
||||
|
||||
endforeach;
|
||||
|
||||
endif;
|
||||
|
||||
return $new_post_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the taxonomies of a post to another post
|
||||
*/
|
||||
function woocommerce_duplicate_post_taxonomies($id, $new_id, $post_type) {
|
||||
global $wpdb;
|
||||
$taxonomies = get_object_taxonomies($post_type); //array("category", "post_tag");
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
$post_terms = wp_get_object_terms($id, $taxonomy);
|
||||
for ($i=0; $i<count($post_terms); $i++) {
|
||||
wp_set_object_terms($new_id, $post_terms[$i]->slug, $taxonomy, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the meta information of a post to another post
|
||||
*/
|
||||
function woocommerce_duplicate_post_meta($id, $new_id) {
|
||||
global $wpdb;
|
||||
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id");
|
||||
|
||||
if (count($post_meta_infos)!=0) {
|
||||
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
|
||||
foreach ($post_meta_infos as $meta_info) {
|
||||
$meta_key = $meta_info->meta_key;
|
||||
$meta_value = addslashes($meta_info->meta_value);
|
||||
$sql_query_sel[]= "SELECT $new_id, '$meta_key', '$meta_value'";
|
||||
}
|
||||
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
|
||||
$wpdb->query($sql_query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Columns for Products page
|
||||
**/
|
||||
|
|
|
@ -316,4 +316,14 @@ function woocommerce_exclude_image_from_product_page_field_save( $post, $attachm
|
|||
|
||||
function woocommerce_exclude_image_from_product_page_field_add( $post_id ) {
|
||||
add_post_meta( $post_id, '_woocommerce_exclude_image', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate a product action
|
||||
*/
|
||||
add_action('admin_action_duplicate_product', 'woocommerce_duplicate_product_action');
|
||||
|
||||
function woocommerce_duplicate_product_action() {
|
||||
include_once('includes/duplicate_product.php');
|
||||
woocommerce_duplicate_product();
|
||||
}
|
|
@ -83,6 +83,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
|
|||
== Changelog ==
|
||||
|
||||
= 1.4 =
|
||||
* Improved default theme
|
||||
* Support for multiple and stacked (compound) taxes
|
||||
* Locale options for country address formatting and checkout fields
|
||||
* Multiple taxes shown in order total tables
|
||||
|
|
|
@ -79,9 +79,6 @@ class woocommerce {
|
|||
// Start a PHP session
|
||||
if (!session_id()) session_start();
|
||||
|
||||
// Output Buffering
|
||||
ob_start();
|
||||
|
||||
// Set up localisation
|
||||
$this->load_plugin_textdomain();
|
||||
|
||||
|
|
Loading…
Reference in New Issue