Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created November 28, 2017 13:03
Show Gist options
  • Save peterjaap/545a4166153cb9f93f5b27a52f6ed81a to your computer and use it in GitHub Desktop.
Save peterjaap/545a4166153cb9f93f5b27a52f6ed81a to your computer and use it in GitHub Desktop.
Helper functions for product import in Magento 2 to create swatch attribute options
<?php
/**
* @param $attributeCode
* @param $newOptionValues
* @internal param $attributeData
*/
public function addSwatchValuesToSwatchAttribute($attributeCode, $newOptionValues)
{
$this->eavConfig->clear();
$attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
if (!$attribute) {
return;
}
$attributeData = [];
$attributeData['option'] = $this->addExistingOptions($attribute);
$existingColors = array_values($attributeData['option']);
foreach ($newOptionValues as $key => $value) {
if (is_numeric($key)) {
$colorLabel = $value;
$colorValue = '#fff'; // Default is white when no hex value is given
} else {
$colorLabel = $key;
// If multiple colors are set, we need image swatches
// For now, we just use the first color
$value = explode('/', $value);
$colorValue = $value[0];
}
if (in_array($colorLabel, $existingColors)) continue;
$newOptionId = $this->insertSwatchOption($attribute, $colorLabel);
$this->colorMap[$colorLabel] = $colorValue;
$attributeData['option'][$newOptionId] = $colorLabel;
}
$this->eavConfig->clear();
$attributeData['frontend_input'] = 'select';
$attributeData['swatch_input_type'] = 'visual';
$attributeData['update_product_preview_image'] = 1;
$attributeData['use_product_image_for_swatch'] = 0;
$attributeData['optionvisual'] = $this->getOptionSwatch($attributeData);
$attributeData['defaultvisual'] = $this->getOptionDefaultVisual($attributeData);
$attributeData['swatchvisual'] = $this->getOptionSwatchVisual($attributeData);
$attribute->addData($attributeData);
$attribute->save();
}
/**
* @param array $attributeData
* @return array
*/
protected function getOptionSwatch(array $attributeData)
{
$optionSwatch = ['order' => [], 'value' => [], 'delete' => []];
$i = 0;
foreach ($attributeData['option'] as $optionKey => $optionValue) {
$optionSwatch['delete'][$optionKey] = '';
$optionSwatch['order'][$optionKey] = (string)$i++;
$optionSwatch['value'][$optionKey] = [$optionValue, ''];
}
return $optionSwatch;
}
/**
* @param array $attributeData
* @return array
*/
private function getOptionSwatchVisual(array $attributeData)
{
$optionSwatch = ['value' => []];
foreach ($attributeData['option'] as $optionKey => $optionValue) {
if (substr($optionValue, 0, 1) == '#' && strlen($optionValue) == 7) {
$optionSwatch['value'][$optionKey] = $optionValue;
} else if (isset($this->colorMap[$optionValue])) {
$optionSwatch['value'][$optionKey] = $this->colorMap[$optionValue];
} else {
/* Fetch swatch value from current swatch option */
$swatches = $this->swatchHelper->getSwatchesByOptionsId([$optionKey]);
if (isset($swatches[$optionKey])) {
$swatch = $swatches[$optionKey];
}
if (isset($swatch) && isset($swatch['value']) && !empty($swatch['value'])) {
$optionSwatch['value'][$optionKey] = $swatch['value'];
} else {
$optionSwatch['value'][$optionKey] = $this->colorMap['White'];
}
}
}
return $optionSwatch;
}
/**
* @param array $attributeData
* @return array
*/
private function getOptionDefaultVisual(array $attributeData)
{
$optionSwatch = $this->getOptionSwatchVisual($attributeData);
if (isset(array_keys($optionSwatch['value'])[0]))
return [array_keys($optionSwatch['value'])[0]];
else
return [''];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment