WPITECHWPITECH

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    12 Best Ecommerce Software in 2021 & in the Future

    July 31, 2021

    Why Is My Canon Printer Not Printing Properly?

    July 31, 2021

    5 Best Google Analytics Plugins

    May 28, 2021
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    WPITECH WPITECH
    • Home
    • Tutorials
      • WordPress Tutorials
      • WordPress Plugin
      • WordPress Themes
      • WooCommerce
      • Ecommerce
      • PHP
    • Hosting
    • Deals
      • Hosting Deals
    • SEO TOOLS
      • Bulk Url Checker
      • Structured Data
      • Google Cache
      • Hyperlink Generator
      • Free URL Checker
    • Marketing
      • SEO
      • Advertising
    • Reviews
    • Write for Us
    • About Us
    WPITECHWPITECH
    Home » How to Customize WooCommerce Product Variations
    WooCommerce

    How to Customize WooCommerce Product Variations

    WPITECHBy WPITECHOctober 13, 2019No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    How-To-Add-WooCommerce-Product-Variations
    Share
    Facebook Twitter LinkedIn Pinterest Email

    WooCommerce is one of the best eCommerce solutions in the market. It helps people to sell various kinds of products easily. WooCommerce is a perfect fit for both developers and store owners.

    What is Product Variation in WooCommerce

    WooCommerce Product variation is considered as different variations of the same product. For example product in different sizes and colors etc.

    When a product has different variations then the product from which the variations are created known as the main product. Each product variation has its own price and product information.

    You can easily display similar products on your store using WooCommerce product variations.

    In this tutorial, I will show you a brief about WooCommerce product variations using an example of a WooCommerce store that sells shirts. All the shirts are the same but they differ in color, design, and attributes.

    Now each product variation has different prices, images, videos, and SKU numbers. However, each WooCommerce product variation is a different entity in the online store.

    How to Create WooCommerce Variable Product with Attributes

    This code defines the WooCommerce variable product ID using a custom function that actually creating a product variation. The parent product of the variable product is required for creating attributes such as the array of value, prices, SKU and stock.

    All the data is stored in a formatted multidimensional array. This function checks whether the value of the attributes already exists and if not then it creates an entry for the WooCommerce product attribute and then it will set in the variable parent product. Add the following code snippet in functions.php file.

    function create_WooCommerce_product_variation( $product_id, $variation_data ){
    $product = woo_get_product($product_id);
    $variation_post = array(
            'post_title'  => $product->get_title(),
            'post_name'   => 'product-'.$product_id.'-variation',
         'post_status' => 'publish',
         'post_parent' => $product_id,
            'post_type'   => 'product_variation',
         'guid'    => $product->get_permalink()
    );
    $variation_id = woo_insert_post( $variation_post );
    $variation = new WooCommerce_Product_Variation( $variation_id );
    foreach ($variation_data['attributes'] as $attribute => $term_name )
    {
         $taxonomy = 'pa_'.$attribute; 
         if( ! taxonomy_exists( $taxonomy ) ){
                register_taxonomy(
                    $taxonomy,
                    'product_variation',
                    array(
                        'hierarchical' => false,
                        'label' => ucfirst( $attribute ),
                        'query_var' => true,
                        'rewrite' => array( 'slug' => sanitize_title($attribute) ), 
                 )
             );
         }
         if( ! term_exists( $term_name, $taxonomy ) )
                woo_insert_term( $term_name, $taxonomy ); 
         $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; 
            $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );
         if( ! in_array( $term_name, $post_term_names ) )
                woo_set_post_terms( $product_id, $term_name, $taxonomy, true );
            update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
    }
    if( ! empty( $variation_data['sku'] ) )
            $variation->set_sku( $variation_data['sku'] );
    if( empty( $variation_data['sale_price'] ) ){
            $variation->set_price( $variation_data['regular_price'] );
    } else {
            $variation->set_price( $variation_data['sale_price'] );
            $variation->set_sale_price( $variation_data['sale_price'] );
    }
        $variation->set_regular_price( $variation_data['regular_price'] );
    if( ! empty($variation_data['stock_qty']) ){
            $variation->set_stock_quantity( $variation_data['stock_qty'] );
            $variation->set_manage_stock(true);
            $variation->set_stock_status('');
    } else {
            $variation->set_manage_stock(false);
    }
        $variation->set_weight(''); 
        $variation->save(); 
    }
    

    woo_get_product($product_id) Gets the WooCommerce variable product object.

    woo_insert_post( $variation_post ) create the WooCommerce product variation.

    WooCommerce_Product_Variation( $variation_id ) create an instance of the WooCommerce_Product_Variation object.

    register_taxonomy will create taxonomy if doesn’t exist.

    woo_insert_term check if the term name exists and If not it will create a new term.

    $post_term_names get the post Terms names from the parent variable WooCommerce product.

    woo_set_post_terms checks if the post-term exists and, if not it will create in the parent variable product.

    update_post_meta saves the attribute data in the WooCommerce product variation

    $variation->set_sku saves all other data in SKU

    $variation_data[‘sale_price’] consist prices values in product variations

    $variation_data[‘stock_qty’] consist stock values in product variations

    $variation->save() saves attribute values of WooCommerce product variations

    Product variation in WooCommerce WooCommerce Product Variation
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    WPITECH

    Related Posts

    5 Best WooCommerce Affiliate Plugins

    January 3, 2020

    How to Customize Add to Cart Button in WooCommerce

    October 21, 2019

    How to Change WooCommerce Payment Status for Prepaid Orders

    October 12, 2019
    Add A Comment

    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Recent Posts
    • 12 Best Ecommerce Software in 2021 & in the Future
    • Why Is My Canon Printer Not Printing Properly?
    • 5 Best Google Analytics Plugins
    • 12 Best Email List Building Tools in 2021
    • 9 Amazing Benefits of Using WordPress for Your Projects
    Categories
    • Advertising (2)
    • Ecommerce (13)
    • Hosting (1)
    • Marketing (12)
    • PHP (3)
    • Reviews (4)
    • SEO (10)
    • Uncategorized (2)
    • WooCommerce (14)
    • WordPress Plugin (4)
    • WordPress Themes (1)
    • WordPress Tutorials (38)
    Editors Picks
    Top Reviews
    Advertisement
    Demo
    WPITECH
    Facebook Twitter Instagram Pinterest Vimeo YouTube
    • Home
    © 2023 All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.