cancel
Showing results for 
Search instead for 
Did you mean: 

create or update product configurable programmatically in magento2

SOLVED

create or update product configurable programmatically in magento2

Hello everyone,
I get this error when I try to run a script that should update the configurable and associated products it takes from an xml file
from what I understand he can not modify them, while if they do not exist, he manages to create them, but does not associate them

 

Fatal error: Uncaught InvalidArgumentException: Provided attribute can not be used with configurable product in /home/agrizodm/public_html/vendor/magento/module-configurable-product/Model/Product/Type/Configurable.php:671
Stack trace:
#0 /home/agrizodm/public_html/vendor/magento/module-configurable-product/Model/Product/Type/Configurable.php(632): Magento\ConfigurableProduct\Model\Product\Type\Configurable->saveConfigurableOptions(Object(\Model\Preference\Product\Product\Interceptor))
#1 /home/agrizodm/public_html/generated/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Interceptor.php(219): Magento\ConfigurableProduct\Model\Product\Type\Configurable->save(Object(\Model\Preference\Product\Product\Interceptor))
#2 /home/agrizodm/public_html/vendor/magento/module-catalog/Model/Product.php(919): Magento\ConfigurableProduct\Model\Product\Type\Configurable\Interceptor->save(Object(\Model\Preference\Product\Product\Interceptor))
#3 / in /home/agrizodm/public_html/vendor/magento/module-configurable-product/Model/Product/Type/Configurable.php on line 671

someone can help me? thanks

3 REPLIES

Re: create or update product configurable programmatically in magento2

Hello,

 

make sure below thing for your attribute

the scope is set to global

attribute is visible
the attribute is user defined
attribute uses a source (which is e.g. the case for dropdown and multi-select attributes)
See \Magento\ConfigurableProduct\Model\Product\Type\Configurable::canUseAttribute.

 

hope it will help you, if works then mark as solution.

Re: create or update product configurable programmatically in magento2

thank you for your answer

i have this code for create product configurable 

$confProduct
->setStoreId(1)//you can set data in store scope
->setWebsiteIds(array(1))//website ID the product is assigned to, as an array
->setAttributeSetId(4)//ID of a attribute set named 'default'
->setTypeId('configurable')//product type
->setCreatedAt(strtotime('now'))//product creation time
->setSku($sku)//SKU
->setName($titolo)//product name
->setWeight($peso_netto)
->setStatus(1)//product status (1 - enabled, 2 - disabled)
->setTaxClassId(2)//tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(4)//catalog and search visibility
->setCondition('nuovo')
->setDescription($desc_est)
->setShortDescription('<p></p>')
->setManufacturer($marca_id);
if($fileIMG!='' && $fileIMG!=NULL) {
$confProduct
->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
->addImageToMediaGallery($fileIMG, array('image', 'thumbnail', 'small_image'), true, false); //assigning image, thumb and small image to media gallery
}

$confProduct->setCanSaveConfigurableAttributes(true);
$confProduct->setCanSaveCustomOptions(true);
$confProduct->getTypeInstance()->setUsedProductAttributeIds($attribute, $confProduct); //attribute ID of attribute 'color' in my store
$configurableAttributesData = $confProduct->getTypeInstance()->getConfigurableAttributesAsArray($confProduct);
$confProduct->setCanSaveConfigurableAttributes(true);
$confProduct->setConfigurableAttributesData($configurableAttributesData);
$confProduct->setConfigurableProductsData($configurableProductsData);

if ($i == 1) {
$confProduct->save();
} 

$_productStock = $stockRegistry->getStockItem($prodID);
$_productStock->setQty(9999); 
$_productStock->setMinSaleQty(1);
$_productStock->setMaxSaleQty(1000);
$_productStock->setIsInStock(1);
$_productStock->setStockId(1);
$_productStock->setStoreId(1);
$_productStock->setManageStock(0);
$_productStock->setVisibility(4);
$_productStock->setManufacturer($marca_id);
if ($i == 1) {
$_productStock->save();
} 

Where should I go to edit with those features?

Re: create or update product configurable programmatically in magento2

Hello @alessio_ottaviano

$confProduct->getTypeInstance()->setUsedProductAttributeIds($attribute, $confProduct); 

where attribute means those attribute are you using for configurable you need to pass it

$attribute = array(134, 135); // Super Attribute Ids Used To Create Configurable Product


hope it will help you.