0

I have an attribute with attribute ID 'color'. Which has options with visual swatches. and another attribute with ID 'material' which has options with Text swatches.

Now for the 'color' attribute I need to add options with swatch image or color code programatically. Also add the option labels stores wise.

For the 'material' attribute I need to add the option labels and description store wise.

Please give me some idea regarding this.

1 Answer 1

1

Code something like the below.

namespace Vendor\Module\Model;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;

class ColorImport
{
    private AttributeFactory $attributeFactory;
    
    public function __construct(AttributeFactory $attributeFactory)
    {
        $this->attributeFactory = $attributeFactory;
    }

    public function execute()
    {
        /** @var ProductAttributeInterface $model */
        $model = $this->attributeFactory->create();
        $model->loadByCode(Product::ENTITY,'color');
        $data = [];
        $data['optionvisual']['value'] = ['option_0' => ['Black', 'Black']];
        $data['swatchvisual']['value'] = ['option_0' => '#000000'];
        $model->addData($data);
        $model->save();
    }
}

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.