66use Magento \CatalogImportExport \Model \Import \Product \Validator ;
77use Magento \Eav \Api \Data \AttributeOptionInterfaceFactory ;
88use Magento \Eav \Model \Config as EavConfig ;
9+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
910
1011class CreateMissingAttributeOptionPlugin
1112{
@@ -43,28 +44,38 @@ public function beforeIsAttributeValid(Validator $subject, $attrCode, array $att
4344
4445 $ attribute = $ this ->eavConfig ->getAttribute ('catalog_product ' , $ attrCode );
4546 // phpcs:ignore
46- if ($ attribute -> getSourceModel () != ' Magento\Eav\Model\Entity\Attribute\Source\Table ' ) {
47+ if (! $ this -> isAttributeApplicable ( $ attribute ) ) {
4748 return [$ attrCode , $ attrParams , $ rowData ];
4849 }
4950
5051 $ values = explode (Product::PSEUDO_MULTI_LINE_SEPARATOR , $ rowData [$ attrCode ]);
52+ $ values = array_filter ($ values , function ($ optionName ) use ($ attrParams ) {
53+ if (!strlen ($ optionName )) {
54+ return false ;
55+ }
56+
57+ $ indexValue = strtolower ($ optionName );
58+ if (isset ($ attrParams ['options ' ][$ indexValue ])) {
59+ return false ;
60+ }
61+
62+ return true ;
63+ });
5164
5265 foreach ($ values as $ value ) {
53- $ optionName = strtolower ($ value );
54- if (!isset ($ attrParams ['options ' ][$ optionName ]) && strlen ($ optionName )) {
55- $ option = $ this ->createAttributeOption ($ attrCode , $ value );
66+ $ indexValue = strtolower ($ value );
67+ $ option = $ this ->createAttributeOption ($ attrCode , $ value );
5668
57- $ attrParams ['options ' ][$ optionName ] = $ option ->getValue ();
58- // Delete Common Attributes Cache, for forcing reloading the Values
59- \Magento \CatalogImportExport \Model \Import \Product \Type \AbstractType::$ commonAttributesCache = [];
60- }
69+ $ attrParams ['options ' ][$ indexValue ] = $ option ->getValue ();
70+ // Delete Common Attributes Cache, for forcing reloading the Values
71+ \Magento \CatalogImportExport \Model \Import \Product \Type \AbstractType::$ commonAttributesCache = [];
6172 }
6273
6374 return [$ attrCode , $ attrParams , $ rowData ];
6475 }
6576
6677 /**
67- * Create a matching attribute option
78+ * Create a matching attribute option.
6879 *
6980 * @param string $attributeCode Attribute the option should exist in
7081 * @param string $label Label to add
@@ -73,6 +84,7 @@ public function beforeIsAttributeValid(Validator $subject, $attrCode, array $att
7384 public function createAttributeOption ($ attributeCode , $ label )
7485 {
7586 $ option = $ this ->findAttributeOptionByLabel ($ attributeCode , $ label );
87+
7688 if (!$ option ) {
7789 $ option = $ this ->optionDataFactory ->create ();
7890 $ option ->setLabel ($ label );
@@ -85,9 +97,11 @@ public function createAttributeOption($attributeCode, $label)
8597 $ this ->eavConfig ->clear ();
8698 $ option = $ this ->findAttributeOptionByLabel ($ attributeCode , $ label );
8799 }
100+
88101 if (!$ option ) {
89102 die ('Could not find ' . $ label . ' in ' . $ attributeCode );
90103 }
104+
91105 return $ option ;
92106 }
93107
@@ -104,6 +118,20 @@ public function findAttributeOptionByLabel($attributeCode, $label)
104118 return $ attributeOptionInterface ;
105119 }
106120 }
121+
107122 return null ;
108123 }
124+
125+ /**
126+ * Returns true if attribute is applicable for option generation.
127+ *
128+ * @param AbstractAttribute $attribute
129+ * @return bool
130+ */
131+ public function isAttributeApplicable (AbstractAttribute $ attribute )
132+ {
133+ $ sourceModel = $ attribute ->getSourceModel ();
134+
135+ return $ sourceModel === null || $ sourceModel === 'Magento\Eav\Model\Entity\Attribute\Source\Table ' ;
136+ }
109137}
0 commit comments