0

I wrote an extension to import products from an XML-file. It is working (for now), but I am using a ->save() method on the Class Magento\ConfigurableProduct\Model\Product\Type\Configurable\AttributeFactory:

$attributeModel = $this->configurableAttributeFactory->create();            
$attributes = [$attributeId];
foreach ($attributes as $key => $attributeId) {
    $data = ['attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $key];
    $attributeModel->setData($data);
    $attributeModel->save(); /* <<< deprecated method */
}

The save() method seems to be deprecated, and I want to update the extension. So, what is the proper alternative? I already changed the saving-method for the products, to use the resource Model, but I dont know, how to change it for the configurable attributes.

1 Answer 1

0

ok, it took me quite some testing, but to save the attribute, you need to use the class Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute

I use

$this->configurableAttribute = Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute

so the resulting code would be:

$attributeModel = $this->configurableAttributeFactory->create();            
$attributes = [$attributeId];
foreach ($attributes as $key => $attributeId) {
    $data = ['attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $key];
    $attributeModel->setData($data);
    $this->configurableAttribute->save($attributeModel);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.