Reading list Switch to dark mode

    Magento2 – Assign Associated Products To Configurable Product

    Updated 16 July 2021

    Assign Associated Products To Configurable Product

    Use the following code snippet to assign already created associated products to configurable product.

    $productId = 12; // Configurable Product Id
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
    $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
    $position = 0;
    $attributes = array(134, 135); // Super Attribute Ids Used To Create Configurable Product
    $associatedProductIds = array(2,4,5,6); //Product Ids Of Associated Products
    foreach ($attributes as $attributeId) {
    	$data = array('attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $position);
    	$position++;
    	$attributeModel->setData($data)->save();
    }
    $product->setTypeId("configurable"); // Setting Product Type As Configurable
    $product->setAffectConfigurableProductAttributes(4);
    $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
    $product->setNewVariationsAttributeSetId(4); // Setting Attribute Set Id
    $product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
    $product->setCanSaveConfigurableAttributes(true);
    $product->save();

    Note : If there is already associated product assigned (same combination) to configurable product. It will throw error.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    10 comments

  • Milind
    Hi,
    It is not working.

    Created simple products for one attributes options. There is no error but in admin, Configurations section is empty.

  • Apktech Company
    Not working.

    Got error Fatal error: Uncaught exception ‘MagentoFrameworkExceptionInputException’ with message ‘Option values are not specified.’ in

  • Ramazan BOSTAN
    Hello,

    It is not working.

    Created simple products for two attributes options. Generate and got all simple products ids.
    There is no error but in admin, Configurations section is empty and

    Ideas?

  • Sadeesh Mani
    Hello,

    i use this code, it’s showing error.

    error:
    Fatal error: Uncaught MagentoFrameworkExceptionInputException: Option values are not specified. in /var/www/html/magento2/vendor/magento/module-configurable-product/Model/OptionRepository.php on line 270

    Please advice…

  • avvishnu
    How pro-grammatically create a configurable Magento 2 product?
  • Back to Top

    Message Sent!

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

    Back to Home