cancel
Showing results for 
Search instead for 
Did you mean: 

How to create OptionInterface from variable for $configurable->setUsedProductOptions()

How to create OptionInterface from variable for $configurable->setUsedProductOptions()

Hi,
I have written a module for an additional API endpoint with the purpose of adding simple SKUs to configurable SKUs. Example:

POST /rest/V1/configurable-products/:sku/children

{"optionAttributeCodes":["size","color"],"variationSkus":["MH01-L-Black","MH01-L-Gray","MH01-L-Orange","MH01-M-Black","MH01-M-Gray","MH01-M-Orange","MH01-S-Black","MH01-S-Gray","MH01-S-Orange","MH01-XL-Black","MH01-XL-Gray","MH01-XL-Orange","MH01-XS-Black","MH01-XS-Gray","MH01-XS-Orange"]}

Before the module, I had written a PHP script which uses a mixture of Magento API endpoints to achieve the desired effect. This includes:

 

 - adding the attributes to the configurable

 - pulling the attribute values from the simple SKUs

 - setting the simples attribute values as options to the configurable

 - setting the simples as associated products to the configurable

 

This works as expected and since writing the script I have now developed a module to achieve all this in 1 API call as the example I initially shown above.

 

Currently, the module achieves everything the script did except for setting the simple attribute values as options to the configurable. The only reason I cannot do this is because I cannot figure out how I create an OptionInterface from my $simpleOptions variable for use with the function:

$configurable->getExtensionAttributes()->setConfigurableProductOptions($simpleOptions);

Below is a json_encoded dump of the $simpleOptions variable that is populated by the module during processing. The data matches the structure used in my previous script which used the following API endpoint to set the options.

POST /rest/V1/configurable-products/:sku/options

{"size":{"option":{"attribute_id":"135","label":"Size","values":[{"value_index":"171"},{"value_index":"170"},{"value_index":"169"},{"value_index":"172"},{"value_index":"168"}]}},"color":{"option":{"attribute_id":"93","label":"Color","values":[{"value_index":"50"},{"value_index":"53"},{"value_index":"57"}]}}}

How do I create an OptionInterface object from my $simpleOptions variable?

/**
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $configurableProductOptions
* @return $this
*/
public function setConfigurableProductOptions($configurableProductOptions);


Many thanks in advance!
Dan!