How to create Configurable Product ProgrammaticallyCan't Create Configurable Products with multiple Attributes programmaticallyError when creating configurable product programmatically in multi-storeview siteMagento 2 – Assign Associated Products To Configurable ProductHow to display configurable product swatches when product is Out of stockConfigurable Product Programmatically Configurations is emptyHow to add custom options programmatically for configurable product in Magento 2Magento 2: How to create configurable product ProgrammaticallyMahento 2 : How to associate more simple products with an existing configurable product programatically?magento 1.9 detach simple product associated to a configurable and assign to another configurableShow out of Stock Configurable products magento2
In general, would I need to season a meat when making a sauce?
A Riley Respite
Why does Mjolnir fall down in Age of Ultron but not in Endgame?
Caught 2 students cheating together on the final exam that I proctored
Is the derivative with respect to a fermion field Grassmann-odd?
Where have Brexit voters gone?
Can a person survive on blood in place of water?
Why were helmets and other body armour not commonplace in the 1800s?
Do photons bend spacetime or not?
Where is the logic in castrating fighters?
Why would Ryanair allow me to book this journey through a third party, but not through their own website?
Externally monitoring CPU/SSD activity without software access
Any advice on creating fictional locations in real places when writing historical fiction?
A steel cutting sword?
Why didn't Thanos use the Time Stone to stop the Avengers' plan?
Compaq Portable vs IBM 5155 Portable PC
Does Nitrogen inside commercial airliner wheels prevent blowouts on touchdown?
What to do when you've set the wrong ISO for your film?
Boss wants me to falsify a report. How should I document this unethical demand?
Why does this if-statement combining assignment and an equality check return true?
Is the taxi route omitted in low visibility (LVP)?
Why do most published works in medical imaging try to reduce false positives?
Should breaking down something like a door be adjudicated as an attempt to beat its AC and HP, or as an ability check against a set DC?
Count Even Digits In Number
How to create Configurable Product Programmatically
Can't Create Configurable Products with multiple Attributes programmaticallyError when creating configurable product programmatically in multi-storeview siteMagento 2 – Assign Associated Products To Configurable ProductHow to display configurable product swatches when product is Out of stockConfigurable Product Programmatically Configurations is emptyHow to add custom options programmatically for configurable product in Magento 2Magento 2: How to create configurable product ProgrammaticallyMahento 2 : How to associate more simple products with an existing configurable product programatically?magento 1.9 detach simple product associated to a configurable and assign to another configurableShow out of Stock Configurable products magento2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have found some question same my own but have not found out the answer. I want to create a configurable product. I have create a form to save the product and get the child product details in configurable-matrix
:
But when I'm trying to save this product, the error Option values are not specified.
appear. Can someone help me to save the product or just release another way to save a configurable product. Thanks!
magento2 configurable-product error programmatically
add a comment |
I have found some question same my own but have not found out the answer. I want to create a configurable product. I have create a form to save the product and get the child product details in configurable-matrix
:
But when I'm trying to save this product, the error Option values are not specified.
appear. Can someone help me to save the product or just release another way to save a configurable product. Thanks!
magento2 configurable-product error programmatically
add a comment |
I have found some question same my own but have not found out the answer. I want to create a configurable product. I have create a form to save the product and get the child product details in configurable-matrix
:
But when I'm trying to save this product, the error Option values are not specified.
appear. Can someone help me to save the product or just release another way to save a configurable product. Thanks!
magento2 configurable-product error programmatically
I have found some question same my own but have not found out the answer. I want to create a configurable product. I have create a form to save the product and get the child product details in configurable-matrix
:
But when I'm trying to save this product, the error Option values are not specified.
appear. Can someone help me to save the product or just release another way to save a configurable product. Thanks!
magento2 configurable-product error programmatically
magento2 configurable-product error programmatically
edited Dec 17 '17 at 10:22
Nero Phung
asked Dec 15 '17 at 7:50
Nero PhungNero Phung
9071720
9071720
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to set attribute id and its value to it. For example this array of data:
public function getProductData($data = [])
$productData = [
'stock_data' => [
'min_qty_allowed_in_shopping_cart' => [
[
'record_id' => "0",
'customer_group_id' => "32000",
'min_sale_qty' => "",
]
],
'deferred_stock_update' => "1",
'use_config_deferred_stock_update' => "1",
'min_qty' => "0",
'max_sale_qty' => "10000",
'notify_stock_qty' => "1",
'qty_increments' => "1",
'min_sale_qty' => "1",
'use_config_manage_stock' => "1",
'manage_stock' => "1",
'use_config_min_qty' => "1",
'use_config_max_sale_qty' => "1",
'use_config_backorders' => "1",
'backorders' => "0",
'use_config_notify_stock_qty' => "1",
'use_config_enable_qty_inc' => "1",
'enable_qty_increments' => "0",
'use_config_qty_increments' => "1",
'use_config_min_sale_qty' => "1",
'is_qty_decimal' => "0",
'is_decimal_divided' => "0",
],
'use_config_is_returnable' => "1",
'gift_wrapping_available' => "1",
'use_config_gift_wrapping_available' => "1",
'gift_message_available' => "0",
'use_config_gift_message_available' => "1",
'links_title' => "Links",
'links_purchased_separately' => "0",
'samples_title' => "Samples",
'attribute_set_id' => "4",
'status' => $this->isParentProductEnable(),
'affect_product_custom_options' => "1",
'name' => $data['title'],
'title_rewrite' => "",
'price' => "100",
'weight' => "",
'url_key' => "",
'gift_wrapping_price' => "",
'special_price' => "",
'cost' => "",
'msrp' => "",
'quantity_and_stock_status' => [
'qty' => "",
'is_in_stock' => "1"
],
'is_returnable' => "2",
'website_ids' => [$this->getWebsiteId($data['division'])],
'tax_class_id' => "2",
'product_has_weight' => "1",
'visibility' => "4",
'color' => "49",
'size' => "91",
'options_container' => "container2",
'weltpixel_exclude_from_sitemap' => "0",
'msrp_display_actual_price_type' => "0",
'description' => "",
'short_description' => "",
'custom_layout_update' => "",
'sku' => $data['product_code'],
'meta_title' => $data['title'],
'meta_keyword' => $data['title'],
'meta_description' => "",
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'associated_product_ids_serialized' => "[]",
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
];
return $productData;
The important part is here:
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
That would be the data indicating the simple products data.
Updated: These are methods for building simple products with configurable product:
private function getConfigurableMatrix($data)
$jsonOutput = "[";
// Get pair codes for matrix
$colors = $data['colors'];
$sizes = $this->getSizeArray($colors)['sizes'];
$status = $this->isChildProductEnable();
foreach ($colors as $color)
foreach ($sizes as $size)
$colorParam = [
'id' => $this->getColorOptionId($color['color_id']),
'lable' => $color['color_id']
];
$sizeParam = [
'id' => $this->getSizeOptionId($size['size']),
'lable' => $size['size']
];
$weight = $size['weight'] ? $size['weight'] : $this->getDefaultWeight();
$jsonOutput .= $this->getConfigurableMatrixItem($data['title'], $size['sku'], $colorParam, $sizeParam, $size['quantity'], $weight, $size['price'], $status) . ",";
$jsonOutput = rtrim($jsonOutput, ',');
$jsonOutput .= "]";
return $jsonOutput;
private function getConfigurableMatrixItem($name, $sku, $color = [], $size = [], $qty, $weight, $price, $status = 1)
$item = [
"id" => null,
"product_link" => "",
"name" => "$name $color['lable'] $size['lable']",
"sku" => "$sku",
"status" => $status,
"price" => "$price",
"price_currency" => "$",
"price_string" => "$$price",
"weight" => "$weight",
"qty" => "$qty",
"variationKey" => "$size['id']-$color['id']",
"configurable_attribute" => ""color":"$color['id']","size":"$size['id']"",
"thumbnail_image" => "",
"media_gallery" => [
"images" => []
],
"image" => [],
"attributes" => "Color : $color['lable'], Size: $size['lable']",
"was_changed" => true,
"canEdit" => 1,
"newProduct" => 1,
"record_id" => 0
];
return json_encode($item);
private function getSizeOptionId($labelSize)
if ($labelSize == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$lableSize][0] = $lableSize;
$option['value']['size'][0] = $labelSize;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
$this->insertSwatch($optionId, 0, $labelSize);
return $optionId;
private function getColorOptionId($labelColor)
if ($labelColor == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$labelColor][0] = $labelColor;
$option['value']['color'][0] = $labelColor;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
$this->insertSwatch($optionId);
return $optionId;
private function insertSwatch($optionId, $type = 1, $value = "#ffffff")
$swatch = $this->createObject('MagentoSwatchesModelSwatch');
$swatch->setData([
'option_id' => $optionId,
'store_id' => 0,
'type' => $type,
'value' => $value
]);
$swatch->save();
How can I call the function:$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
|
show 2 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f205923%2fhow-to-create-configurable-product-programmatically%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to set attribute id and its value to it. For example this array of data:
public function getProductData($data = [])
$productData = [
'stock_data' => [
'min_qty_allowed_in_shopping_cart' => [
[
'record_id' => "0",
'customer_group_id' => "32000",
'min_sale_qty' => "",
]
],
'deferred_stock_update' => "1",
'use_config_deferred_stock_update' => "1",
'min_qty' => "0",
'max_sale_qty' => "10000",
'notify_stock_qty' => "1",
'qty_increments' => "1",
'min_sale_qty' => "1",
'use_config_manage_stock' => "1",
'manage_stock' => "1",
'use_config_min_qty' => "1",
'use_config_max_sale_qty' => "1",
'use_config_backorders' => "1",
'backorders' => "0",
'use_config_notify_stock_qty' => "1",
'use_config_enable_qty_inc' => "1",
'enable_qty_increments' => "0",
'use_config_qty_increments' => "1",
'use_config_min_sale_qty' => "1",
'is_qty_decimal' => "0",
'is_decimal_divided' => "0",
],
'use_config_is_returnable' => "1",
'gift_wrapping_available' => "1",
'use_config_gift_wrapping_available' => "1",
'gift_message_available' => "0",
'use_config_gift_message_available' => "1",
'links_title' => "Links",
'links_purchased_separately' => "0",
'samples_title' => "Samples",
'attribute_set_id' => "4",
'status' => $this->isParentProductEnable(),
'affect_product_custom_options' => "1",
'name' => $data['title'],
'title_rewrite' => "",
'price' => "100",
'weight' => "",
'url_key' => "",
'gift_wrapping_price' => "",
'special_price' => "",
'cost' => "",
'msrp' => "",
'quantity_and_stock_status' => [
'qty' => "",
'is_in_stock' => "1"
],
'is_returnable' => "2",
'website_ids' => [$this->getWebsiteId($data['division'])],
'tax_class_id' => "2",
'product_has_weight' => "1",
'visibility' => "4",
'color' => "49",
'size' => "91",
'options_container' => "container2",
'weltpixel_exclude_from_sitemap' => "0",
'msrp_display_actual_price_type' => "0",
'description' => "",
'short_description' => "",
'custom_layout_update' => "",
'sku' => $data['product_code'],
'meta_title' => $data['title'],
'meta_keyword' => $data['title'],
'meta_description' => "",
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'associated_product_ids_serialized' => "[]",
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
];
return $productData;
The important part is here:
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
That would be the data indicating the simple products data.
Updated: These are methods for building simple products with configurable product:
private function getConfigurableMatrix($data)
$jsonOutput = "[";
// Get pair codes for matrix
$colors = $data['colors'];
$sizes = $this->getSizeArray($colors)['sizes'];
$status = $this->isChildProductEnable();
foreach ($colors as $color)
foreach ($sizes as $size)
$colorParam = [
'id' => $this->getColorOptionId($color['color_id']),
'lable' => $color['color_id']
];
$sizeParam = [
'id' => $this->getSizeOptionId($size['size']),
'lable' => $size['size']
];
$weight = $size['weight'] ? $size['weight'] : $this->getDefaultWeight();
$jsonOutput .= $this->getConfigurableMatrixItem($data['title'], $size['sku'], $colorParam, $sizeParam, $size['quantity'], $weight, $size['price'], $status) . ",";
$jsonOutput = rtrim($jsonOutput, ',');
$jsonOutput .= "]";
return $jsonOutput;
private function getConfigurableMatrixItem($name, $sku, $color = [], $size = [], $qty, $weight, $price, $status = 1)
$item = [
"id" => null,
"product_link" => "",
"name" => "$name $color['lable'] $size['lable']",
"sku" => "$sku",
"status" => $status,
"price" => "$price",
"price_currency" => "$",
"price_string" => "$$price",
"weight" => "$weight",
"qty" => "$qty",
"variationKey" => "$size['id']-$color['id']",
"configurable_attribute" => ""color":"$color['id']","size":"$size['id']"",
"thumbnail_image" => "",
"media_gallery" => [
"images" => []
],
"image" => [],
"attributes" => "Color : $color['lable'], Size: $size['lable']",
"was_changed" => true,
"canEdit" => 1,
"newProduct" => 1,
"record_id" => 0
];
return json_encode($item);
private function getSizeOptionId($labelSize)
if ($labelSize == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$lableSize][0] = $lableSize;
$option['value']['size'][0] = $labelSize;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
$this->insertSwatch($optionId, 0, $labelSize);
return $optionId;
private function getColorOptionId($labelColor)
if ($labelColor == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$labelColor][0] = $labelColor;
$option['value']['color'][0] = $labelColor;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
$this->insertSwatch($optionId);
return $optionId;
private function insertSwatch($optionId, $type = 1, $value = "#ffffff")
$swatch = $this->createObject('MagentoSwatchesModelSwatch');
$swatch->setData([
'option_id' => $optionId,
'store_id' => 0,
'type' => $type,
'value' => $value
]);
$swatch->save();
How can I call the function:$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
|
show 2 more comments
You have to set attribute id and its value to it. For example this array of data:
public function getProductData($data = [])
$productData = [
'stock_data' => [
'min_qty_allowed_in_shopping_cart' => [
[
'record_id' => "0",
'customer_group_id' => "32000",
'min_sale_qty' => "",
]
],
'deferred_stock_update' => "1",
'use_config_deferred_stock_update' => "1",
'min_qty' => "0",
'max_sale_qty' => "10000",
'notify_stock_qty' => "1",
'qty_increments' => "1",
'min_sale_qty' => "1",
'use_config_manage_stock' => "1",
'manage_stock' => "1",
'use_config_min_qty' => "1",
'use_config_max_sale_qty' => "1",
'use_config_backorders' => "1",
'backorders' => "0",
'use_config_notify_stock_qty' => "1",
'use_config_enable_qty_inc' => "1",
'enable_qty_increments' => "0",
'use_config_qty_increments' => "1",
'use_config_min_sale_qty' => "1",
'is_qty_decimal' => "0",
'is_decimal_divided' => "0",
],
'use_config_is_returnable' => "1",
'gift_wrapping_available' => "1",
'use_config_gift_wrapping_available' => "1",
'gift_message_available' => "0",
'use_config_gift_message_available' => "1",
'links_title' => "Links",
'links_purchased_separately' => "0",
'samples_title' => "Samples",
'attribute_set_id' => "4",
'status' => $this->isParentProductEnable(),
'affect_product_custom_options' => "1",
'name' => $data['title'],
'title_rewrite' => "",
'price' => "100",
'weight' => "",
'url_key' => "",
'gift_wrapping_price' => "",
'special_price' => "",
'cost' => "",
'msrp' => "",
'quantity_and_stock_status' => [
'qty' => "",
'is_in_stock' => "1"
],
'is_returnable' => "2",
'website_ids' => [$this->getWebsiteId($data['division'])],
'tax_class_id' => "2",
'product_has_weight' => "1",
'visibility' => "4",
'color' => "49",
'size' => "91",
'options_container' => "container2",
'weltpixel_exclude_from_sitemap' => "0",
'msrp_display_actual_price_type' => "0",
'description' => "",
'short_description' => "",
'custom_layout_update' => "",
'sku' => $data['product_code'],
'meta_title' => $data['title'],
'meta_keyword' => $data['title'],
'meta_description' => "",
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'associated_product_ids_serialized' => "[]",
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
];
return $productData;
The important part is here:
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
That would be the data indicating the simple products data.
Updated: These are methods for building simple products with configurable product:
private function getConfigurableMatrix($data)
$jsonOutput = "[";
// Get pair codes for matrix
$colors = $data['colors'];
$sizes = $this->getSizeArray($colors)['sizes'];
$status = $this->isChildProductEnable();
foreach ($colors as $color)
foreach ($sizes as $size)
$colorParam = [
'id' => $this->getColorOptionId($color['color_id']),
'lable' => $color['color_id']
];
$sizeParam = [
'id' => $this->getSizeOptionId($size['size']),
'lable' => $size['size']
];
$weight = $size['weight'] ? $size['weight'] : $this->getDefaultWeight();
$jsonOutput .= $this->getConfigurableMatrixItem($data['title'], $size['sku'], $colorParam, $sizeParam, $size['quantity'], $weight, $size['price'], $status) . ",";
$jsonOutput = rtrim($jsonOutput, ',');
$jsonOutput .= "]";
return $jsonOutput;
private function getConfigurableMatrixItem($name, $sku, $color = [], $size = [], $qty, $weight, $price, $status = 1)
$item = [
"id" => null,
"product_link" => "",
"name" => "$name $color['lable'] $size['lable']",
"sku" => "$sku",
"status" => $status,
"price" => "$price",
"price_currency" => "$",
"price_string" => "$$price",
"weight" => "$weight",
"qty" => "$qty",
"variationKey" => "$size['id']-$color['id']",
"configurable_attribute" => ""color":"$color['id']","size":"$size['id']"",
"thumbnail_image" => "",
"media_gallery" => [
"images" => []
],
"image" => [],
"attributes" => "Color : $color['lable'], Size: $size['lable']",
"was_changed" => true,
"canEdit" => 1,
"newProduct" => 1,
"record_id" => 0
];
return json_encode($item);
private function getSizeOptionId($labelSize)
if ($labelSize == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$lableSize][0] = $lableSize;
$option['value']['size'][0] = $labelSize;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
$this->insertSwatch($optionId, 0, $labelSize);
return $optionId;
private function getColorOptionId($labelColor)
if ($labelColor == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$labelColor][0] = $labelColor;
$option['value']['color'][0] = $labelColor;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
$this->insertSwatch($optionId);
return $optionId;
private function insertSwatch($optionId, $type = 1, $value = "#ffffff")
$swatch = $this->createObject('MagentoSwatchesModelSwatch');
$swatch->setData([
'option_id' => $optionId,
'store_id' => 0,
'type' => $type,
'value' => $value
]);
$swatch->save();
How can I call the function:$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
|
show 2 more comments
You have to set attribute id and its value to it. For example this array of data:
public function getProductData($data = [])
$productData = [
'stock_data' => [
'min_qty_allowed_in_shopping_cart' => [
[
'record_id' => "0",
'customer_group_id' => "32000",
'min_sale_qty' => "",
]
],
'deferred_stock_update' => "1",
'use_config_deferred_stock_update' => "1",
'min_qty' => "0",
'max_sale_qty' => "10000",
'notify_stock_qty' => "1",
'qty_increments' => "1",
'min_sale_qty' => "1",
'use_config_manage_stock' => "1",
'manage_stock' => "1",
'use_config_min_qty' => "1",
'use_config_max_sale_qty' => "1",
'use_config_backorders' => "1",
'backorders' => "0",
'use_config_notify_stock_qty' => "1",
'use_config_enable_qty_inc' => "1",
'enable_qty_increments' => "0",
'use_config_qty_increments' => "1",
'use_config_min_sale_qty' => "1",
'is_qty_decimal' => "0",
'is_decimal_divided' => "0",
],
'use_config_is_returnable' => "1",
'gift_wrapping_available' => "1",
'use_config_gift_wrapping_available' => "1",
'gift_message_available' => "0",
'use_config_gift_message_available' => "1",
'links_title' => "Links",
'links_purchased_separately' => "0",
'samples_title' => "Samples",
'attribute_set_id' => "4",
'status' => $this->isParentProductEnable(),
'affect_product_custom_options' => "1",
'name' => $data['title'],
'title_rewrite' => "",
'price' => "100",
'weight' => "",
'url_key' => "",
'gift_wrapping_price' => "",
'special_price' => "",
'cost' => "",
'msrp' => "",
'quantity_and_stock_status' => [
'qty' => "",
'is_in_stock' => "1"
],
'is_returnable' => "2",
'website_ids' => [$this->getWebsiteId($data['division'])],
'tax_class_id' => "2",
'product_has_weight' => "1",
'visibility' => "4",
'color' => "49",
'size' => "91",
'options_container' => "container2",
'weltpixel_exclude_from_sitemap' => "0",
'msrp_display_actual_price_type' => "0",
'description' => "",
'short_description' => "",
'custom_layout_update' => "",
'sku' => $data['product_code'],
'meta_title' => $data['title'],
'meta_keyword' => $data['title'],
'meta_description' => "",
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'associated_product_ids_serialized' => "[]",
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
];
return $productData;
The important part is here:
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
That would be the data indicating the simple products data.
Updated: These are methods for building simple products with configurable product:
private function getConfigurableMatrix($data)
$jsonOutput = "[";
// Get pair codes for matrix
$colors = $data['colors'];
$sizes = $this->getSizeArray($colors)['sizes'];
$status = $this->isChildProductEnable();
foreach ($colors as $color)
foreach ($sizes as $size)
$colorParam = [
'id' => $this->getColorOptionId($color['color_id']),
'lable' => $color['color_id']
];
$sizeParam = [
'id' => $this->getSizeOptionId($size['size']),
'lable' => $size['size']
];
$weight = $size['weight'] ? $size['weight'] : $this->getDefaultWeight();
$jsonOutput .= $this->getConfigurableMatrixItem($data['title'], $size['sku'], $colorParam, $sizeParam, $size['quantity'], $weight, $size['price'], $status) . ",";
$jsonOutput = rtrim($jsonOutput, ',');
$jsonOutput .= "]";
return $jsonOutput;
private function getConfigurableMatrixItem($name, $sku, $color = [], $size = [], $qty, $weight, $price, $status = 1)
$item = [
"id" => null,
"product_link" => "",
"name" => "$name $color['lable'] $size['lable']",
"sku" => "$sku",
"status" => $status,
"price" => "$price",
"price_currency" => "$",
"price_string" => "$$price",
"weight" => "$weight",
"qty" => "$qty",
"variationKey" => "$size['id']-$color['id']",
"configurable_attribute" => ""color":"$color['id']","size":"$size['id']"",
"thumbnail_image" => "",
"media_gallery" => [
"images" => []
],
"image" => [],
"attributes" => "Color : $color['lable'], Size: $size['lable']",
"was_changed" => true,
"canEdit" => 1,
"newProduct" => 1,
"record_id" => 0
];
return json_encode($item);
private function getSizeOptionId($labelSize)
if ($labelSize == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$lableSize][0] = $lableSize;
$option['value']['size'][0] = $labelSize;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
$this->insertSwatch($optionId, 0, $labelSize);
return $optionId;
private function getColorOptionId($labelColor)
if ($labelColor == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$labelColor][0] = $labelColor;
$option['value']['color'][0] = $labelColor;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
$this->insertSwatch($optionId);
return $optionId;
private function insertSwatch($optionId, $type = 1, $value = "#ffffff")
$swatch = $this->createObject('MagentoSwatchesModelSwatch');
$swatch->setData([
'option_id' => $optionId,
'store_id' => 0,
'type' => $type,
'value' => $value
]);
$swatch->save();
You have to set attribute id and its value to it. For example this array of data:
public function getProductData($data = [])
$productData = [
'stock_data' => [
'min_qty_allowed_in_shopping_cart' => [
[
'record_id' => "0",
'customer_group_id' => "32000",
'min_sale_qty' => "",
]
],
'deferred_stock_update' => "1",
'use_config_deferred_stock_update' => "1",
'min_qty' => "0",
'max_sale_qty' => "10000",
'notify_stock_qty' => "1",
'qty_increments' => "1",
'min_sale_qty' => "1",
'use_config_manage_stock' => "1",
'manage_stock' => "1",
'use_config_min_qty' => "1",
'use_config_max_sale_qty' => "1",
'use_config_backorders' => "1",
'backorders' => "0",
'use_config_notify_stock_qty' => "1",
'use_config_enable_qty_inc' => "1",
'enable_qty_increments' => "0",
'use_config_qty_increments' => "1",
'use_config_min_sale_qty' => "1",
'is_qty_decimal' => "0",
'is_decimal_divided' => "0",
],
'use_config_is_returnable' => "1",
'gift_wrapping_available' => "1",
'use_config_gift_wrapping_available' => "1",
'gift_message_available' => "0",
'use_config_gift_message_available' => "1",
'links_title' => "Links",
'links_purchased_separately' => "0",
'samples_title' => "Samples",
'attribute_set_id' => "4",
'status' => $this->isParentProductEnable(),
'affect_product_custom_options' => "1",
'name' => $data['title'],
'title_rewrite' => "",
'price' => "100",
'weight' => "",
'url_key' => "",
'gift_wrapping_price' => "",
'special_price' => "",
'cost' => "",
'msrp' => "",
'quantity_and_stock_status' => [
'qty' => "",
'is_in_stock' => "1"
],
'is_returnable' => "2",
'website_ids' => [$this->getWebsiteId($data['division'])],
'tax_class_id' => "2",
'product_has_weight' => "1",
'visibility' => "4",
'color' => "49",
'size' => "91",
'options_container' => "container2",
'weltpixel_exclude_from_sitemap' => "0",
'msrp_display_actual_price_type' => "0",
'description' => "",
'short_description' => "",
'custom_layout_update' => "",
'sku' => $data['product_code'],
'meta_title' => $data['title'],
'meta_keyword' => $data['title'],
'meta_description' => "",
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'associated_product_ids_serialized' => "[]",
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
];
return $productData;
The important part is here:
'configurable_attributes_data' => [
self::COLOR_ATTRIBUTE_ID => [
'attribute_id' => self::COLOR_ATTRIBUTE_ID,
'code' => "color",
'label' => "Color",
'position' => "0",
'values' => $this->getColorValue($data)
],
self::SIZE_ATTRIBUTE_ID => [
'attribute_id' => self::SIZE_ATTRIBUTE_ID,
'code' => "size",
'label' => "Size",
'position' => "1",
'values' => $this->getSizeValue($data)
]
],
'configurable-matrix-serialized' => $this->getConfigurableMatrix($data)
That would be the data indicating the simple products data.
Updated: These are methods for building simple products with configurable product:
private function getConfigurableMatrix($data)
$jsonOutput = "[";
// Get pair codes for matrix
$colors = $data['colors'];
$sizes = $this->getSizeArray($colors)['sizes'];
$status = $this->isChildProductEnable();
foreach ($colors as $color)
foreach ($sizes as $size)
$colorParam = [
'id' => $this->getColorOptionId($color['color_id']),
'lable' => $color['color_id']
];
$sizeParam = [
'id' => $this->getSizeOptionId($size['size']),
'lable' => $size['size']
];
$weight = $size['weight'] ? $size['weight'] : $this->getDefaultWeight();
$jsonOutput .= $this->getConfigurableMatrixItem($data['title'], $size['sku'], $colorParam, $sizeParam, $size['quantity'], $weight, $size['price'], $status) . ",";
$jsonOutput = rtrim($jsonOutput, ',');
$jsonOutput .= "]";
return $jsonOutput;
private function getConfigurableMatrixItem($name, $sku, $color = [], $size = [], $qty, $weight, $price, $status = 1)
$item = [
"id" => null,
"product_link" => "",
"name" => "$name $color['lable'] $size['lable']",
"sku" => "$sku",
"status" => $status,
"price" => "$price",
"price_currency" => "$",
"price_string" => "$$price",
"weight" => "$weight",
"qty" => "$qty",
"variationKey" => "$size['id']-$color['id']",
"configurable_attribute" => ""color":"$color['id']","size":"$size['id']"",
"thumbnail_image" => "",
"media_gallery" => [
"images" => []
],
"image" => [],
"attributes" => "Color : $color['lable'], Size: $size['lable']",
"was_changed" => true,
"canEdit" => 1,
"newProduct" => 1,
"record_id" => 0
];
return json_encode($item);
private function getSizeOptionId($labelSize)
if ($labelSize == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$lableSize][0] = $lableSize;
$option['value']['size'][0] = $labelSize;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'size');
$optionId = $attribute->getSource()->getOptionId($labelSize);
$this->insertSwatch($optionId, 0, $labelSize);
return $optionId;
private function getColorOptionId($labelColor)
if ($labelColor == '') return "";
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
if (!$optionId)
// Create a new Option
$option['attribute_id'] = $attribute->getId();
//$option['value'][$labelColor][0] = $labelColor;
$option['value']['color'][0] = $labelColor;
$eavSetup = $this->_eavSetupFactory->create();
$eavSetup->addAttributeOption($option);
$this->_eavConfig = $this->createObject('MagentoEavModelConfig');
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color');
$optionId = $attribute->getSource()->getOptionId($labelColor);
$this->insertSwatch($optionId);
return $optionId;
private function insertSwatch($optionId, $type = 1, $value = "#ffffff")
$swatch = $this->createObject('MagentoSwatchesModelSwatch');
$swatch->setData([
'option_id' => $optionId,
'store_id' => 0,
'type' => $type,
'value' => $value
]);
$swatch->save();
edited Dec 18 '17 at 6:40
answered Dec 16 '17 at 2:18
Toàn TamToàn Tam
424316
424316
How can I call the function:$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
|
show 2 more comments
How can I call the function:$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
How can I call the function:
$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
How can I call the function:
$this->getConfigurableMatrix($data)
– Nero Phung
Dec 16 '17 at 8:41
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
This is my function to build the json data for simple product. Do you want to see it ?
– Toàn Tam
Dec 18 '17 at 2:50
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
Of course yes!!
– Nero Phung
Dec 18 '17 at 5:28
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
please, see my updated answer
– Toàn Tam
Dec 18 '17 at 6:40
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
Any luck @NeroPhung ?
– Toàn Tam
Dec 18 '17 at 9:43
|
show 2 more comments
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f205923%2fhow-to-create-configurable-product-programmatically%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown