/** * Create a matching attribute option * * @param string $attributeCode Attribute the option should exist in * @param string $label Label to add * @return \Magento\Eav\Api\Data\AttributeOptionInterface */ public function createAttributeOption($attributeCode, $label) { $option = $this->findAttributeOptionByLabel($attributeCode, $label); if (!$option) { $option = $this->optionDataFactory->create(); $option->setLabel($label); $this->attributeOptionManagementInterface->add($attributeCode, $option); $option = $this->findAttributeOptionByLabel($attributeCode, $label); } return $option; }
/** * Convert option values from arrays to data objects * * @param array $options * @return \Magento\Eav\Api\Data\AttributeOptionInterface[] */ protected function convertToObjects(array $options) { $dataObjects = []; foreach ($options as $option) { /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $optionDataObject */ $optionDataObject = $this->optionDataFactory->create(); $this->dataObjectHelper->populateWithArray($optionDataObject, $option, '\\Magento\\Eav\\Api\\Data\\AttributeOptionInterface'); $dataObjects[] = $optionDataObject; } return $dataObjects; }
protected function updateAll($updateData) { $result = 0; foreach ($updateData as $attributeUpdate) { if ($attributeUpdate['attribute_type'] == 'product') { if ($attributeUpdate['status'] == self::ADD_ATTRIBUTE_OPTION) { $optionToAdd = $this->optionDataFactory->create(); $optionToAdd->setLabel($attributeUpdate['value'])->setSortOrder(0)->setIsDefault(0); try { $this->attributeOptionManagement->add($attributeUpdate['attribute_code'], $optionToAdd); $result++; } catch (\Exception $e) { $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Unable to add attribute option', 'Error: ' . $e->getMessage()); $result = false; } } else { if ($attributeUpdate['status'] == self::AUTO_REMOVE_ATTRIBUTE_OPTION) { try { $this->attributeOptionManagement->delete($attributeUpdate['attribute_code'], $attributeUpdate['option_id']); $result++; } catch (\Exception $e) { $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Unable to remove attribute option', 'Error: ' . $e->getMessage()); $result = false; } } } } elseif ($attributeUpdate['attribute_type'] == 'global_setting') { $this->carrierConfigHandler->saveConfig('carriers/shipper/' . $attributeUpdate['attribute_code'], $attributeUpdate['value']); } } if ($result >= 0) { $this->checkSynchStatus(true); } return $result; }