Woocommerce How To Duplicate A Product And Keep Comments

WooCommerce: How to Duplicate a Product AND Keep Your Precious Comments!

So, you’re a WooCommerce whiz (or aspire to be one!), and you’ve crafted the *perfect* product page. Now you need to create a similar product – maybe a different color, size, or material variation. You think, “Great, I’ll just duplicate it!” But wait… you also have a ton of valuable customer reviews and comments on the original product. You definitely don’t want to lose those!

This guide will walk you through duplicating your WooCommerce product *and* keeping those precious comments, even if you’re just starting out. We’ll cover why you might want to do this, the common pitfalls, and the easiest methods.

Why Duplicate a Product (And Keep the Comments!)?

Imagine this: You sell awesome handmade leather wallets. You have a popular “Classic Brown” wallet with glowing reviews. Now, you’re introducing a “Midnight Black” version.

    • Save Time: Recreating the entire product page from scratch? Ain’t nobody got time for that! Duplicating lets you keep the core description, images (which you can easily change, of course!), and even related product settings.
    • Consistency: Maintain a consistent look and feel across your product catalog. A duplicated product will inherit the original’s layout and design elements, saving you from accidentally creating inconsistencies.
    • Keep the Social Proof!: Positive reviews and comments build trust and encourage sales. Imagine having a duplicate product without *any* reviews. It looks brand new and untested. Keeping the comments (especially if they are good) provides instant credibility. Let’s say a customer asks a question about the “Classic Brown” wallet in the comments; it’s highly likely that question is relevant to the “Midnight Black” version as well!
    • SEO Boost: Duplicating and slightly modifying a successful product can leverage its existing SEO juice. While you’ll need to optimize the duplicate for its specific attributes (like “Midnight Black leather wallet” instead of “Classic Brown leather wallet”), you’re starting from a stronger base than a completely new product.

    The Default WooCommerce Duplication: A Common Pitfall

    WooCommerce has a basic “Duplicate” feature (usually a “Copy to a new draft” option on the product listing page). However, this method does not carry over comments. It creates a brand new product *without* the comment history. This is the frustrating realization many users have!

    Method 1: Using the “Duplicate Post” Plugin (Easiest for Beginners)

    The easiest and most reliable way to duplicate products with comments is to use a plugin. The “Duplicate Post” plugin is a free and widely used option.

    1. Install and Activate the Plugin: Go to Plugins > Add New in your WordPress dashboard and search for “Duplicate Post.” Install and activate it.

    2. Configure the Plugin (Optional, but Recommended): Go to Settings > Duplicate Post. The default settings usually work well, but you might want to adjust:

    • “What to copy”: Choose which elements to copy during duplication (e.g., title, content, excerpt, featured image, etc.). Ensure “Comments” is checked!
    • “Permissions”: Control who can duplicate posts (e.g., Administrators, Editors, Authors).

    3. Duplicate Your Product: Go to Products in your WooCommerce dashboard. Hover over the product you want to duplicate. You should now see a “Clone” or “Duplicate” link. Click it!

    4. Edit the Duplicate: The plugin will create a draft copy of your product. Open it and:

    • Change the Title and Permalink: This is crucial for SEO to avoid duplicate content issues. Change the product name to reflect the variation (e.g., “Midnight Black Leather Wallet”). Click the “Edit” button next to the permalink and update it too.
    • Update the Description and Images: Modify the product description and images to match the new variation. Don’t just copy and paste everything! Add unique details.
    • Adjust Pricing and Attributes: Update the price, attributes (e.g., color, size), inventory, and any other relevant settings.
    • Publish! Once you’re happy with the changes, publish the product.

    Why this works: The “Duplicate Post” plugin is designed to create a *true* copy of your product, including the associated metadata that stores comments. It’s simple, reliable, and widely compatible.

    Method 2: Code Snippet (For the More Technically Inclined)

    If you’re comfortable with PHP, you can add a code snippet to your theme’s `functions.php` file (or, better yet, use a code snippets plugin to avoid theme updates overwriting your code). Warning: Be careful when editing your functions.php file. One mistake can break your website. Back up your website before making any changes!

     <?php 

    function duplicate_woocommerce_product_with_comments( $post_id ) {

    // Get the original post object

    $post = get_post( $post_id );

    if ( isset( $_GET[‘duplicate_post_as_draft’] ) && $_GET[‘duplicate_post_as_draft’] == ‘true’ ) {

    if (! ( isset( $_GET[‘post’]) || isset($_POST[‘post’]) || ( isset($_REQUEST[‘action’]) && ‘duplicate_post_as_draft’ == $_REQUEST[‘action’] ) ) ) {

    wp_die(‘No post to duplicate has been supplied!’);

    }

    /*

    * Nonce verification

    */

    if ( !isset( $_GET[‘duplicate_nonce’] ) || ! wp_verify_nonce( $_GET[‘duplicate_nonce’], basename(__FILE__) ) )

    return;

    /*

    * get the original post id

    */

    $post_id = (isset($_GET[‘post’]) ? absint( $_GET[‘post’] ) : absint( $_POST[‘post’] ) );

    /*

    * and all the original post data then

    */

    $post = get_post( $post_id );

    Read more about How To Add Cart To Woocommerce

    /*

    * if you don’t want current user to be the new post author,

    * then change next line to fix the author id to 1.

    * example: $new_post_author = 1;

    */

    $current_user = wp_get_current_user();

    $new_post_author = $current_user->ID;

    /*

    * if post data exists, then create the post duplicate

    */

    if (isset( $post ) && $post != null) {

    /*

    * new post data array

    */

    $args = array(

    ‘comment_status’ => $post->comment_status,

    ‘ping_status’ => $post->ping_status,

    ‘post_author’ => $new_post_author,

    ‘post_content’ => $post->post_content,

    ‘post_excerpt’ => $post->post_excerpt,

    ‘post_name’ => $post->post_name,

    ‘post_parent’ => $post->post_parent,

    ‘post_password’ => $post->post_password,

    ‘post_status’ => ‘draft’,

    ‘post_title’ => $post->post_title,

    ‘post_type’ => $post->post_type,

    ‘to_ping’ => $post->to_ping,

    ‘menu_order’ => $post->menu_order

    );

    /*

    * insert the post by wp_insert_post() function

    */

    $new_post_id = wp_insert_post( $args );

    /*

    * duplicate all post meta just in case

    */

    $post_meta_infos = get_post_meta($post_id);

    if (count($post_meta_infos)!=0) {

    foreach ($post_meta_infos as $key => $values) {

    foreach ($values as $value) {

    $value = maybe_unserialize( $value );

    add_post_meta( $new_post_id, $key, $value );

    }

    }

    }

    /*

    * Read more about How To Subtract Fee From Preorder Total Woocommerce duplicate all taxonomies just in case

    */

    $taxonomies = get_object_taxonomies($post->post_type);

    foreach ($taxonomies as $taxonomy) {

    $post_terms = wp_get_object_terms($post_id, $taxonomy, array(‘fields’ => ‘slugs’));

    wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);

    }

    // Duplicate comments

    $comments = get_comments(array(‘post_id’ => $post_id));

    foreach($comments as $comment) {

    $commentdata = array(

    ‘comment_post_ID’ => $new_post_id,

    ‘comment_author’ => $comment->comment_author,

    ‘comment_author_email’ => $comment->comment_author_email,

    ‘comment_author_url’ => $comment->comment_author_url,

    ‘comment_content’ => $comment->comment_content,

    ‘comment_type’ => $comment->comment_type,

    ‘comment_parent’ => $comment->comment_parent,

    ‘user_id’ => $comment->user_id,

    ‘comment_date’ => $comment->comment_date,

    ‘comment_approved’ => $comment->comment_approved,

    );

    wp_insert_comment($commentdata);

    }

    /*

    * finally, redirect to the edit post screen for the new draft

    */

    wp_redirect( admin_url( ‘post.php?action=edit&post=’ . $new_post_id ) );

    exit;

    } else {

    wp_die(‘Post creation failed, could not find original post: ‘ . $post_id);

    }

    }

    }

    add_action( ‘admin_action_duplicate_post_as_draft’, ‘duplicate_woocommerce_product_with_comments’ );

    /*

    * Add the duplicate link to action list for post_row_actions

    */

    function duplicate_post_link( $actions, $post ) {

    if ($post->post_type == ‘product’) {

    $actions[‘duplicate’] = ‘ID, basename(__FILE__), ‘duplicate_nonce’ ) . ‘” title=”Duplicate this item” rel=”permalink”>Duplicate‘;

    }

    return $actions;

    }

    add_filter(‘post_row_actions’, ‘duplicate_post_link’, 10, 2);

    ?>

    How to Use:

    1. Add the Code: Paste the code into your `functions.php` file or a code snippets plugin.

    2. Go to Products: Navigate to your WooCommerce products in the WordPress dashboard.

    3. Find “Duplicate”: You should see a new “Duplicate” link under each product in the product listing.

    4. Click and Edit: Click the “Duplicate” link. This will create a draft copy of the product, including the comments. Remember to change the title, permalink, description, images, and other relevant details.

    Why this works: This code snippet essentially does the same thing as the plugin, but in code form. It iterates through the comments associated with the original product and creates new comments linked to the duplicate product.

    Important Notes:

    • Testing is Key: Before making any changes to a live site, test these methods on a staging site. This will prevent potential problems.
    • SEO Best Practices: Always change the title, permalink, and description of the duplicate product to avoid duplicate content penalties.
    • Image Optimization: Ensure that your images are optimized for the web (compressed) to improve page load speed.
    • Consider Variations: If you’re just creating variations (e.g., different sizes or colors) of the *same* product, consider using WooCommerce’s built-in product variation feature instead of duplicating the entire product. It’s a more elegant and efficient solution for simple variations.

Duplicating products in WooCommerce can save you a ton of time and effort. By using the “Duplicate Post” plugin or the code snippet, you can easily retain those valuable comments and maintain a consistent product catalog. Good luck, and happy selling!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *