Пример #1
0
 protected function setUp()
 {
     $helper = new ObjectManager($this);
     $this->configurableAttribute = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute')->setMethods(['setData', 'getData', 'addData', '__wakeup', 'getId', 'setId', 'getAttributeId', 'setAttributeId', 'setValues'])->disableOriginalConstructor()->getMock();
     $this->option = $this->getMockBuilder('Magento\\ConfigurableProduct\\Service\\V1\\Data\\Option')->setMethods(['__toArray', 'getValues', 'getAttributeId', 'getPosition', 'isUseDefault', 'getLabel'])->disableOriginalConstructor()->getMock();
     $this->valueConverter = $this->getMockBuilder('Magento\\ConfigurableProduct\\Service\\V1\\Data\\Option\\ValueConverter')->setMethods(['convertArrayFromData'])->disableOriginalConstructor()->getMock();
     $this->value = $this->getMockBuilder('Magento\\ConfigurableProduct\\Service\\V1\\Data\\Option\\Value')->disableOriginalConstructor()->getMock();
     $this->attributeFactory = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\AttributeFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->attributeFactory->expects($this->any())->method('create')->will($this->returnValue($this->configurableAttribute));
     $this->converter = $helper->getObject('Magento\\ConfigurableProduct\\Service\\V1\\Data\\OptionConverter', ['attributeFactory' => $this->attributeFactory, 'valueConverter' => $this->valueConverter]);
 }
 /**
  * Create configurable product options
  *
  * @param array $attributesData
  * @return OptionInterface[]
  * @throws \InvalidArgumentException
  */
 public function create(array $attributesData)
 {
     $options = [];
     foreach ($attributesData as $item) {
         $attribute = $this->attributeFactory->create();
         $eavAttribute = $this->productAttributeRepository->get($item[Attribute::KEY_ATTRIBUTE_ID]);
         if (!$this->productType->canUseAttribute($eavAttribute)) {
             throw new \InvalidArgumentException('Provided attribute can not be used with configurable product.');
         }
         $this->updateAttributeData($attribute, $item);
         $options[] = $attribute;
     }
     return $options;
 }
 /**
  * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  */
 public function testUpdateNoSuchEntityException()
 {
     $productSku = 'productSku';
     $optionId = 3;
     $this->productRepositoryMock->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->productMock));
     $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(ConfigurableType::TYPE_CODE));
     $this->confAttributeFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->attributeMock));
     $this->attributeMock->expects($this->once())->method('load')->with($this->equalTo($optionId));
     $this->attributeMock->expects($this->any())->method('getId')->will($this->returnValue(0));
     $this->writeService->update($productSku, $optionId, $this->optionMock);
 }
Пример #4
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($sku, OptionInterface $option)
 {
     /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
     $configurableAttribute = $this->configurableAttributeFactory->create();
     if ($option->getId()) {
         /** @var Product $product */
         $product = $this->getProduct($sku);
         $configurableAttribute->load($option->getId());
         if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
             throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId()));
         }
         $configurableAttribute->addData($option->getData());
         $configurableAttribute->setValues($option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices());
         try {
             $configurableAttribute->save();
         } catch (\Exception $e) {
             throw new CouldNotSaveException(__('Could not update option with id "%1"', $option->getId()));
         }
     } else {
         $this->validateNewOptionData($option);
         /** @var Product $product */
         $product = $this->productRepository->get($sku);
         $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
         if (!in_array($product->getTypeId(), $allowedTypes)) {
             throw new \InvalidArgumentException('Incompatible product type');
         }
         $eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
         $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
         if ($configurableAttribute->getId()) {
             throw new CouldNotSaveException(__('Product already has this option'));
         }
         $configurableAttributesData = ['attribute_id' => $option->getAttributeId(), 'position' => $option->getPosition(), 'use_default' => $option->getIsUseDefault(), 'label' => $option->getLabel(), 'values' => $option->getValues()];
         try {
             $product->setTypeId(ConfigurableType::TYPE_CODE);
             $product->setConfigurableAttributesData([$configurableAttributesData]);
             $product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
             $product->save();
         } catch (\Exception $e) {
             throw new CouldNotSaveException(__('An error occurred while saving option'));
         }
         $configurableAttribute = $this->configurableAttributeFactory->create();
         $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
     }
     if (!$configurableAttribute->getId()) {
         throw new CouldNotSaveException(__('An error occurred while saving option'));
     }
     return $configurableAttribute->getId();
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function update($productSku, $optionId, Option $option)
 {
     $product = $this->getProduct($productSku);
     $configurableAttribute = $this->configurableAttributeFactory->create();
     $configurableAttribute->load($optionId);
     if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
         throw new NoSuchEntityException('Option with id "%1" not found', [$optionId]);
     }
     $configurableAttribute = $this->optionConverter->getModelFromData($option, $configurableAttribute);
     try {
         $configurableAttribute->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not update option with id "%1"', [$optionId]);
     }
     return true;
 }
Пример #6
0
 /**
  * @param Option $option
  * @param Attribute $configurableAttribute
  * @return Attribute
  */
 public function getModelFromData(Option $option, Attribute $configurableAttribute)
 {
     /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $returnConfigurableAttribute */
     $returnConfigurableAttribute = $this->attributeFactory->create();
     $returnConfigurableAttribute->setData($configurableAttribute->getData());
     $returnConfigurableAttribute->addData($option->__toArray());
     $returnConfigurableAttribute->setId($configurableAttribute->getId());
     $returnConfigurableAttribute->setAttributeId($configurableAttribute->getAttributeId());
     $returnConfigurableAttribute->setValues($configurableAttribute->getPrices());
     $values = $option->getValues();
     if (!is_null($values)) {
         $prices = [];
         foreach ($values as $value) {
             $prices[] = $this->valueConverter->convertArrayFromData($value);
         }
         $returnConfigurableAttribute->setValues($prices);
     }
     return $returnConfigurableAttribute;
 }
Пример #7
0
 /**
  * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Factory::create
  */
 public function testCreate()
 {
     $attributeId = 90;
     $valueIndex = 12;
     $item = ['attribute_id' => $attributeId, 'values' => [['value_index' => $valueIndex]]];
     $data = [$item];
     $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['setValues', 'setData', '__wakeup'])->getMock();
     $this->attributeFactory->expects(static::once())->method('create')->willReturn($attribute);
     $eavAttribute = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock();
     $this->productAttributeRepository->expects(static::once())->method('get')->with($attributeId)->willReturn($eavAttribute);
     $this->configurable->expects(static::once())->method('canUseAttribute')->with($eavAttribute)->willReturn(true);
     $option = $this->getMock(OptionValueInterface::class);
     $option->expects(static::once())->method('setValueIndex')->with($valueIndex)->willReturnSelf();
     $this->optionValueFactory->expects(static::once())->method('create')->willReturn($option);
     $attribute->expects(static::once())->method('setData')->with($item);
     $attribute->expects(static::once())->method('setValues')->with([$option]);
     $result = $this->factory->create($data);
     static::assertSame([$attribute], $result);
 }
Пример #8
0
 /**
  * Delete data specific for Configurable product type
  *
  * @param \Magento\Catalog\Model\Product $product
  */
 public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
 {
     $this->_typeConfigurableFactory->create()->saveProducts($product, []);
     /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
     $configurableAttribute = $this->_configurableAttributeFactory->create();
     $configurableAttribute->deleteByProduct($product);
 }