Woo Shop – how to duplicate products - Blog | createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.
    Tags

    Woo Shop – how to duplicate products

    Woo Shop – how to duplicate products

    It is important to have some example products added when creating a new ecommerce shop using the WooCommerce plugin. You can test how the layout looks, the sorting functionality or the use of pagination on product listing. Adding a new simple product from Admin Area is straightforward: add a title, description, product image and price. For variable products more work is needed, you will need to define attributes, and configure parameters for each variation separately.

    Duplicate variations using a plugin

    The first option will be to use a plugin from the official WordPress plugin repository. Plugins usually use wp_insert_post() and clone all product meta attributes. A new option called “Duplicate item” will be added to Admin Edit Screen List. The following plugins should do the job: “Duplicate Variations for WooCommerce” by Emran Ahmed or “WooCommerce Easy Duplicate Product” by WPGem.

    Import products from CSV

    We’re building a shop, but don’t have any products added yet? A quick solution will be to use “WooCommerce Sample Data”. Samples include definitions of multiple demo products (with images).  A CSV or XML file is placed in: /plugins/woocommerce/sample-data/ . The WordPress importer can be used to import new products. More info can be found in the official Codex documentation: https://docs.woocommerce.com/document/importing-woocommerce-sample-data/

    Clone products on the fly

    You’ve added a couple of real products with a detailed description and beautiful photos, but you don’t want to physically duplicate them. The reason is that copying items seems like additional hustle and you want to keep the database clean.

    There is a solution for this: use the posts_results filter and clone ‘products’ on the fly. The database will remain untouched, you can define how many products should be “virtually cloned” and products will be duplicated on initial request.

    In the attached code snippet, we have 4 real WooCommerce products added via the admin panel, 9 products per page defined, and  the clone multiplier is set to 15. The results will be 60 items on product listing and working pagination. There is an additional  option to limit the total number of products ( $limit_total_products variable ). This code can be useful in the “testing phase”, it’s recommended to remove it in the “production environment”.

    /**
     * Demo purpose - fake products
     * (virtual cloning on the fly)
     */
    add_filter( 'posts_results', 'ct_woo_clone_products', 10, 2 );
    function ct_woo_clone_products( $posts, $query) {
        if ( $query->is_post_type_archive( 'product' ) || $query->is_page( wc_get_page_id( 'shop' ) ) ):
            $paged                   = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
            $ordering                = WC()->query->get_catalog_ordering_args();
            $tmp = explode(' ', $ordering['orderby']);
            $ordering['orderby']     = array_shift($tmp);
            $ordering['orderby']     = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
            /*
             * SETUP
             */
            $real_products_count = 4;
            $products_per_page = 9;
            $limit_total_products = 59;
            $clone_multiplier = 15;
            if(empty($posts)){
                // for virtual pagination
                $params = array(
                    'posts_per_page' => $real_products_count,
                    'post_type' => 'product',
                );
                $posts = get_posts($params);
            };
            $i = 1;
            $counter = 0;
            $cloned_posts = [];
            while ($i <= $clone_multiplier ) {
                foreach ($posts as $post) {
                    $cloned_posts[] = $post;
                    $counter++;
                    if(!is_null($limit_total_products) && $counter == ($limit_total_products - $real_products_count)){
                        break;
                    }
                }
                $i++;
            }
            // overwrite original posts with cloned one
            $posts = $cloned_posts;
            $total2 = count($posts);
            $max_num_pages = ceil($total2 / $products_per_page );
            wc_set_loop_prop('current_page', $paged);
            wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
            wc_set_loop_prop('per_page', $products_per_page);
            wc_set_loop_prop('total', $total2);
            wc_set_loop_prop('total_pages', $max_num_pages);
            // pagination - slice elements
            if($paged == $max_num_pages){
                // last page
                $last_page_total = $total2 % $products_per_page;
                $posts = array_slice($posts, $paged, $last_page_total);
            } else {
                $posts = array_slice($posts, $paged, $products_per_page);
            }
        endif;
        return $posts;
    }

    Generate fake products

    WP-CLI, the command line interface for WP, can be used to introduce multiple “fake products” into a WooCommerce Shop. Wp-cli-faker by Yoast offers the option to use one command that will generate hundreds of products with random attributes. Usage: wp faker WooCommerce products. More info: https://github.com/Yoast/wp-cli-faker

    We hope you find these hints useful. Make sure to follow us for other tips and guidelines.

    Comments
    0 response

    Add comment

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

    Popular news

    Automating stock and price updates in WooCommerce
    • Dev Tips and Tricks

    Automating stock and price updates in WooCommerce

    September 23, 2024 by createIT
    Integrating advanced subscription features in WooCommerce
    • Dev Tips and Tricks

    Integrating advanced subscription features in WooCommerce

    September 16, 2024 by createIT
    Fetching Time records from ActiveCollab API
    • Dev Tips and Tricks

    Fetching Time records from ActiveCollab API

    September 9, 2024 by createIT
    Docker Compose for PrestaShop
    • Dev Tips and Tricks

    Docker Compose for PrestaShop

    September 2, 2024 by createIT
    WordPress wizard in admin – step by step
    • Dev Tips and Tricks

    WordPress wizard in admin – step by step

    August 29, 2024 by createIT
    Order Status Sync between PrestaShop and External APIs
    • Dev Tips and Tricks

    Order Status Sync between PrestaShop and External APIs

    August 26, 2024 by createIT
    What is PHP used for in web development 
    • Dev Tips and Tricks

    What is PHP used for in web development 

    August 22, 2024 by createIT
    Automating WooCommerce product availability date
    • Dev Tips and Tricks

    Automating WooCommerce product availability date

    August 15, 2024 by createIT
    WP Quiz Adventure – FAQ
    • Dev Tips and Tricks

    WP Quiz Adventure – FAQ

    August 12, 2024 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us