skip to Main Content

In Magento 2.4.1, and I have created a php script (not module) to add configurable product.

I want to add configurable product with attributes color & size. I already have sample products setup and I have info of their product ids, which one to go in which config products. I thought I just need to assign simple product ids as associated product, but that didn’t worked out.

I have read a few articles where they say I need to add options data first. So, I did below code, but that also not working:

$productId = $productObjectManager->getIdBySku($sku);
$product = null;
if ($product) {
    $product = $productObjectManager->load($productId);
} else {
    $product = $productObjectManager;
    $product->setTypeId(MagentoConfigurableProductModelProductTypeConfigurable::TYPE_CODE);
    $product->setAttributeSetId($productObjectManager->getDefaultAttributeSetId());
    $product->setSku($sku);
}

$product->setStoreId(0);
$product->setWebsiteIds($websiteIds);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$product->setName($name);
$product->setPrice($price);
$product->setWeight($weight);
$product->setTaxClassId(0); // None
$product->setStockData([
    'use_config_manage_stock' => 1, 
    'is_in_stock' => 1
]);

$configurableAttributesData = [];
$position = 0;
foreach ($configurableAttributes as $configurableAttributeId => $configurableAttribute) {
    $configurableAttributesData[] = [
        'attribute_id' => $configurableAttribute->getId(),
        'code' => $configurableAttribute->getAttributeCode(),
        'label' => $configurableAttribute->getStoreLabel(),
        'position' => $position,
        'values' => $configurableAttributeValues[$configurableAttribute->getAttributeCode()],
    ];
    $position++;
}

//echo '<pre>';print_r($configurableAttributesData);die;

$optionsFactory = $objectManager->create(MagentoConfigurableProductHelperProductOptionsFactory::class);
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($configurableProducts);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$product->save();
echo 'Added';

I am passing $configurableAttributesData data as below:

Array
(
    [0] => Array
        (
            [attribute_id] => 93
            [code] => color
            [label] => Colour
            [position] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [label] => Colour
                            [attribute_id] => 93
                            [value_index] => Black
                        )

                )

        )

    [1] => Array
        (
            [attribute_id] => 140
            [code] => size
            [label] => Size
            [position] => 1
            [values] => Array
                (
                    [0] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 3
                        )

                    [1] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 4
                        )

                    [2] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 5
                        )

                    [3] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 6
                        )

                    [4] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 7
                        )

                    [5] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 8
                        )

                    [6] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 9
                        )

                    [7] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 10
                        )

                    [8] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 11
                        )

                    [9] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 12
                        )

                    [10] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 13
                        )

                    [11] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 14
                        )

                )

        )

)

Can anyone please help!

2

Answers


  1. You can do it by creating custom extension or with Magento 2 API.

    Login or Signup to reply.
  2. Read this article. It is a very detailed description of what you will need to do.

    https://meetanshi.com/blog/programmatically-create-configurable-product-in-magento-2/

    Good luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search