Assign Simple Product to Configurable Product Programmatically

In this article, I will show to how to Assign Simple Product to Configurable Product Programmatically. Magento 2 does not allow you to assign existing simple products to existing configurable products directly from the admin panel. The typical workaround involves creating a placeholder option in the configurable product’s configuration, which you can later replace with the desired simple product.

That solution is not ideal, as it involves too many unnecessary steps. A programmatic approach to perform this task would be more efficient and useful.

Before implementing the programmatic approach, it’s important to have a solid understanding of the different product types in Magento. Creating products programmatically allows developers to set up online stores more efficiently. When a product requires multiple options, such as different colors or sizes, a configurable product is needed in Magento.

Mainly we can create 6 types of products Programmatically in Magento 2:

  • Simple Product
  • Configurable Product
  • Grouped Product
  • Virtual Product
  • Bundle Product
  • Downloadable Product

You can see more details about these types of products here.

Assign Simple Product to Configurable Product Programmatically

Before implementing this, you must create all the simple products (associated products) with a combination of configurable attribute values.

<?php
use Magento\Framework\AppInterface;
try {
    require_once __DIR__ . '/app/bootstrap.php';
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $configurableProductId = 2377  //Replace this product ID with your actual configurable product ID
    $simpleProductIds = [2378, 2379, 2380, 2381]; //Replace these product IDs with your actual associated/simple product IDs
    $attributeIds = [93, 144]; // Configurable Attribute IDs
    $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
    $configurableProduct = $productRepository->getById($configurableProductId);
    $usedProductAttributeIds = $configurableProduct->getTypeInstance()->getUsedProductAttributeIds($configurableProduct);
    $productRepository->save($configurableProduct);
    $configurableAttributesData = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray($configurableProduct);
    if (!$configurableAttributesData) {
        $configurableProduct->getTypeInstance()->setUsedProductAttributeIds($attributeIds, $configurableProduct);
        $configurableAttributesData = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray($configurableProduct);
        $configurableProduct->setConfigurableAttributesData($configurableAttributesData);
    }
    $linkData = [];
    foreach ($simpleProductIds as $simpleProductId) {
        $linkData[] = [
            'id' => $simpleProductId,
            'price' => 0,
        ];
    }
    $configurableProduct->setAssociatedProductIds($simpleProductIds);
    $configurableProduct->setCanSaveConfigurableAttributes(true);
    $configurableProduct->setConfigurableProductsData($linkData);
    $configurableProduct->save();
    echo "Products have been successfully assigned to the configurable product.";
} catch (\Exception $e) {
    echo $e->getMessage();
}

In the above code snippet, replace the $configurableProductId with your configurable product ID with which you want to associate the products, and replace $simpleProductIds with all the simple product IDs you want to associate with the config product and replace $attributeIds with an array of attribute IDs which are configurable attributes like Color, Size, etc.

You may also like this:

Conclusion:

The solution involves programmatically assigning simple products to a configurable product in Magento 2, ensuring that each combination of configurable attributes (like size, color) is unique. The code checks for duplicates before assignment to prevent conflicts, and after successfully assigning products, it displays a confirmation message listing the SKUs of the products that were added. This approach ensures a smooth and error-free assignment process, making it easy to manage and verify configurable product associations.

Rate this post