Reading list Switch to dark mode

    How to add product variation in Shopware6

    Updated 26 November 2021

    Introduction

    In this blog, we will discuss how to add a product variation in Shopware6 at the coding end after creating a product.

    Coding Overview

    I hope you are aware of product variation in Shopware6.

    Sending Product Variation Request

    We need to get a request to have a property option value id with a combination of other option value id. Like we select a Color property value as Blue and another Size property value 30. Then we combine both different property value id in a single array and we can select and create many combinations of property values like this, and pass the parent product id in the request also and other values we can pass which you want to not inherit with parent product. If you choose two options values from every two properties then your combination of variation will be four like matrix calculation.
    please see the screenshot for better understanding https://prnt.sc/20xb5f4 and https://prnt.sc/20xb6zi

    Handling Variation Request

    we need to get product variation data in our controller. Now we get the HTTP request from the client-side and send it to the server-side. We arrange the option value array with persisting data manner. We loop the over incoming data request. please see the below code.

    foreach ($variantCollection as $variant) {
                
                $id = Uuid::randomHex();
                $variantIds[] = $id;
    
                $variant['id'] = $id;
                $variant['active'] = true;
    
                $options = $variant['options'];
                $variant['options'] = [];
    
                foreach($options as $option) {
                    array_push($variant['options'], [
                        'id' => $option
                    ]);
                }
                $productRepository->create([$variant], $context);
            }

    then make an entry for the product_configurator_setting entity, please follow the below code.

    Start your headless eCommerce
    now.
    Find out More
    foreach ($options as $option) {
     $productConfiguratorRepository = $this->container->get('product_configurator_setting.repository');
     $optionData = array(
                   'productId' => $data['product_id'],
                   'optionId' => $option,
                   );
             try{
                  $productConfiguratorRepository->create([$optionData], $context);
                } catch(Exception $ex){
                    $ex->getMessage();
                }
       }

    Multi-Seller Marketplace Plugin

    Now we have done. I hope it will help you. Happy coding 🙂

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home