A configurable product is one of the product types in Magento 2. It consists of a number of simple products, each with a single unique option like color, material, size, and so on, that a customer selects from a drop-down list at the product page. Moreover, each option configuration has its unique stock keeping unit (SKU), which makes such products easily tracked in the inventory.
In this article, we will explore how to, step-by-step, create a configurable product in Magento 2.
Table of contents:
Step 1: Create simple products
Step 2: Create a configurable product
Step 3: Connect simple products with the configurable one
Step 4: Add pictures to a product
Before we commence to going over each step, it is important to mention that you should already have an attribute with options created as well as the category that we will place our new configurable product into.
Step 1: Create simple products
Begin with creating the needed number of simple products that the future configurable product will consist of.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$simpleProduct = $this->productFactory->create(); $simpleProduct->setSku('sku-simple'); $simpleProduct->setName('Simple Product'); $simpleProduct->setAttributeSetId(9); $simpleProduct->setColor(49); // Set attribute $simpleProduct->setWeight(1); $simpleProduct->setStatus(Status::STATUS_ENABLED); $simpleProduct->setTypeId(Type::TYPE_SIMPLE); $simpleProduct->setPrice(10); $simpleProduct->setWebsiteIds([1]); $simpleProduct->setCategoryIds([6]); $simpleProduct->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE); $simpleProduct->setStockData([ 'is_in_stock' => Stock::STOCK_IN_STOCK, //Stock Availability 'qty' => 100 //qty ]); $simpleProduct->getResource()->save($simpleProduct); |
Step 2: Create a configurable product
As simple products are created, proceed with creating a configurable product itself.
1 2 3 4 5 6 7 8 9 10 11 12 |
$configurableProduct = $this->productFactory->create(); $configurableProduct->setSku('sku-configurable'); $configurableProduct->setName('Configurable Product'); $configurableProduct->setAttributeSetId(9); $configurableProduct->setStatus(Status::STATUS_ENABLED); $configurableProduct->setTypeId(TypeConfigurable::TYPE_CODE); $configurableProduct->setWebsiteIds([1]); $configurableProduct->setCategoryIds([6]); $configurableProduct->setVisibility(Visibility::VISIBILITY_BOTH); $configurableProduct->setStockData([ 'is_in_stock' => Stock::STOCK_IN_STOCK, //Stock Availability ] ); |
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.Step 3: Connect simple products with the configurable product
First, connect the attribute to a configurable product.
1 2 3 4 5 |
$attribute = $this->attributeRepository->get('color'); $configurableProduct->getTypeInstance()->setUsedProductAttributeIds( [$attribute->getAttributeId()], $configurableProduct ); |
Second, make simple products associated with the configurable product.
1 2 3 4 5 6 |
$configurableAttributesData = $configurableProduct->getTypeInstance() ->getConfigurableAttributesAsArray($configurableProduct); $configurableProduct->setAssociatedProductIds([$simpleProduct->getId()]); $configurableProduct->setCanSaveConfigurableAttributes(true); $configurableProduct->setConfigurableAttributesData($configurableAttributesData); $configurableProduct->getResource()->save($configurableProduct); |
Configurable Group Products for Magento 2
To bring working with configurable products to a new level, get our module
Download hereStep 4: Add pictures to a product
Adding pictures to a product is not an obligatory, yet it would be very helpful to your customers. This piece of code allows to add pictures to products of any type.
1 2 3 |
$imagePath = "image.jpg"; $product->addImageToMediaGallery($imagePath, ['image', 'small_image', 'thumbnail'], false, false); $product->getResource()->save($product); |
Wrapping it up
This is how to create a configurable product in Magento 2 in three simple steps. To learn how to manage all types of product pages from the admin panel, read our article Magento 2 for Store’s Admin: Create and Customize Products Pages.