1

I have created the 2 attributes in the Admin 1. Color 2. Material.

I'm going to add the attribute options with swatch image programatically. For this I have created the custom code.

Import Color:

public function importColorAttribute($csvFinaldata, &$connection)
{
    try {
        $i = 0;
        foreach ($csvFinaldata as $rowData) {
            $order["option_{$i}"] = $i;
            $optionsStore["option_{$i}"] = [
                0 => $rowData['option_label'],
                1 => $rowData['option_label_us'],
                2 => $rowData['option_label_ca'],
                3 => $rowData['option_label_au'],
                4 => $rowData['option_label_uk'],
                5 => $rowData['option_label_eu']
            ];
            $visualSwatch["option_{$i}"] = $rowData['swatch_data'];
            $delete["option_{$i}"] = '';
            $i++;
        }

        $data =  [
            'optionvisual' => [
                'order' => $order,
                'value' => $optionsStore,
                'delete' => $delete,
            ],
            'swatchvisual' => [
                'value' => $visualSwatch,
            ],
        ];

        foreach ($data as $key => $attributeOptionsData) {
            if ($key == "swatchvisual") {
                $mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
                $swatchVisualFiles = isset($attributeOptionsData['value'])
                ? $attributeOptionsData['value']
                : [];
                $mediaPath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
                foreach ($swatchVisualFiles as $index => $swatchVisualFile) {
                    $mainDir = mb_substr($swatchVisualFile, 0, 1, "UTF-8");
                    $subDir = mb_substr($swatchVisualFile, 1, 1, "UTF-8");
                    if ($mainDir !== '#') {
                        $tmpMediaPath = $this->mediaConfig->getBaseTmpMediaPath() . DIRECTORY_SEPARATOR . $mainDir . DIRECTORY_SEPARATOR . $subDir;
                        $fullTmpMediaPath = $mediaDirectory->getAbsolutePath($tmpMediaPath);
                        $this->filesystemDriver->createDirectory($fullTmpMediaPath);
                        if (!empty($swatchVisualFile)) {
                            if (file_exists($mediaPath . DIRECTORY_SEPARATOR . 'wysiwyg' . DIRECTORY_SEPARATOR . 'color-swatches' . DIRECTORY_SEPARATOR . $swatchVisualFile)) {
                                $this->filesystemDriver->copy(
                                    $mediaPath . DIRECTORY_SEPARATOR . 'wysiwyg' . DIRECTORY_SEPARATOR . 'color-swatches' . DIRECTORY_SEPARATOR . $swatchVisualFile,
                                    $fullTmpMediaPath . DIRECTORY_SEPARATOR . $swatchVisualFile
                                );

                                $newFile = $this->swatchMediaHelper->moveImageFromTmp(DIRECTORY_SEPARATOR . $mainDir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . $swatchVisualFile);
                                if (substr($newFile, 0, 1) == '.') {
                                    $newFile = substr($newFile, 1);
                                }
                                $this->swatchMediaHelper->generateSwatchVariations($newFile);
                                $data[$key]['value'][$index] = $newFile;
                            } else {
                                $data[$key]['value'][$index] = "";
                            }
                        }
                    }
                }
            }
        }
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'color');
        $attribute->addData($data)->save();
        return $this;
    } catch (\Exception $e) {
        throw new LocalizedException(__($e->getMessage()));
    }
}

Import Material:

public function importMaterialAttribute($csvFinaldata, &$connection)
{
    try {
        $i = 0;
        foreach ($csvFinaldata as $rowData) {
            $order["option_{$i}"] = $i;
            $textSwatch["option_{$i}"] = [
                0 => $rowData['swatch_text'],
                1 => $rowData['swatch_text_us'],
                2 => $rowData['swatch_text_ca'],
                3 => $rowData['swatch_text_au'],
                4 => $rowData['swatch_text_uk'],
                5 => $rowData['swatch_text_eu']
            ];
            $optionsStore["option_{$i}"] = [
                0 => $rowData['description_text'],
                1 => $rowData['description_text_us'],
                2 => $rowData['description_text_ca'],
                3 => $rowData['description_text_au'],
                4 => $rowData['description_text_uk'],
                5 => $rowData['description_text_eu'],
            ];
            $delete["option_{$i}"] = '';
            $i++;
        }

        $data =  [
            'optiontext' => [
                'order' => $order,
                'value' => $optionsStore,
                'delete' => $delete,
            ],
            'swatchtext' => [
                'value' => $textSwatch,
            ],
        ];
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'material');
        $attribute->addData($data)->save();
        return $this;
    } catch (\Exception $e) {
        throw new LocalizedException(__($e->getMessage()));
    }
}

Now I want to update the label and swatch images if already option is exist there.

Also if I pass flag to delete then delete that option.

Please give me some suggestion how can I update and delete the already exist option.

0

1 Answer 1

0

can you please try this How to create product visual swatch attribute programmatically with images in 2.4.6

If this answer works as your expected, please mark it as accepted. That indicates your issue is solved and makes it easier for others with the same issue to find a verified answer. And that would be great if you click the upvote button for the answer in case you feel it is useful.

Happy Coding

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