Skip to content

Commit

Permalink
Merge pull request #14 from snowio/fix/swatch-options
Browse files Browse the repository at this point in the history
Save swatch attribute options
  • Loading branch information
Liam Toohey authored May 12, 2020
2 parents 6650f0e + 7a97ac7 commit fcd60fb
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions Model/CodedAttributeOptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,56 @@
use SnowIO\AttributeOptionCode\Api\Data\CodedAttributeOptionInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface as MagentoAttributeOption;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\OptionManagement as ProductOptionManagement;
use Magento\Eav\Model\Config;
use Magento\Catalog\Model\Product;

class CodedAttributeOptionRepository implements CodedAttributeOptionRepositoryInterface
{
/** @var AttributeOptionManagementInterface */
private $optionManagementService;
/** @var AttributeOptionCodeRepository */
private $optionCodeRepository;
/** @var OptionConverter */
private $optionConverter;
/** @var ProductAttributeRepositoryInterface */
private $productAttributeRepository;


/** @var ProductOptionManagement */
private $productOptionManagement;
/** @var Config */
private $eavConfig;

/**
* CodedAttributeOptionRepository constructor.
* @param AttributeOptionManagementInterface $optionManagementService
* @param AttributeOptionCodeRepository $optionCodeRepository
* @param OptionConverter $optionConverter
* @param ProductAttributeRepositoryInterface $productAttributeRepository
* @param ProductOptionManagement $productOptionManagement
* @param Config $eavConfig
*/
public function __construct(
AttributeOptionManagementInterface $optionManagementService,
AttributeOptionCodeRepository $optionCodeRepository,
OptionConverter $optionConverter,
ProductAttributeRepositoryInterface $productAttributeRepository
ProductAttributeRepositoryInterface $productAttributeRepository,
ProductOptionManagement $productOptionManagement,
Config $eavConfig
) {
$this->optionManagementService = $optionManagementService;
$this->optionCodeRepository = $optionCodeRepository;
$this->optionConverter = $optionConverter;
$this->productAttributeRepository = $productAttributeRepository;
$this->productOptionManagement = $productOptionManagement;
$this->eavConfig = $eavConfig;
}

/**
* @param int $entityType
* @param string $attributeCode
* @param CodedAttributeOptionInterface $option
* @throws \Magento\Framework\Exception\InputException
*/
public function save($entityType, $attributeCode, CodedAttributeOptionInterface $option)
{
$optionCode = $option->getValue();
Expand All @@ -40,10 +69,17 @@ public function save($entityType, $attributeCode, CodedAttributeOptionInterface
}
}

/**
* @param int $entityType
* @param string $attributeCode
* @param string $optionCode
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
public function delete($entityType, $attributeCode, $optionCode)
{
$optionId = $this->optionCodeRepository->getOptionId($entityType, $attributeCode, $optionCode);

if (null === $optionId) {
return;
}
Expand All @@ -57,9 +93,28 @@ public function delete($entityType, $attributeCode, $optionCode)
$this->optionCodeRepository->removeOption($entityType, $attributeCode, $optionCode, $optionId);
}

/**
* @param $entityType
* @param $attributeCode
* @param $optionCode
* @param MagentoAttributeOption $option
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
private function addOption($entityType, $attributeCode, $optionCode, MagentoAttributeOption $option)
{
$newOptionId = $this->optionManagementService->add($entityType, $attributeCode, $option);
if ($this->eavConfig->getEntityType($entityType)->getEntityTypeCode() === Product::ENTITY) {
/**
* If attribute option is for product entity type, call the product add method directly.
* This must be called directly to ensure the swatch plugin is executed.
* If the swatch plugin is not executed, swatch attribute options are not saved correctly.
*
* @see \Magento\Swatches\Plugin\Eav\Model\Entity\Attribute\OptionManagement::beforeAdd
*/
$newOptionId = $this->productOptionManagement->add($attributeCode, $option);
} else {
$newOptionId = $this->optionManagementService->add($entityType, $attributeCode, $option);
}

// Magento returns the new option id prefixed with "id_"
// We remove non numeric characters to get the correct option id
Expand All @@ -68,6 +123,13 @@ private function addOption($entityType, $attributeCode, $optionCode, MagentoAttr
$this->optionCodeRepository->setOptionId($entityType, $attributeCode, $optionCode, $newOptionId);
}

/**
* @param $attributeCode
* @param MagentoAttributeOption $magentoOption
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
public function updateOption($attributeCode, MagentoAttributeOption $magentoOption)
{
$attribute = $this->productAttributeRepository->get($attributeCode);
Expand Down

0 comments on commit fcd60fb

Please sign in to comment.