コード例 #1
0
 public function testGetModelFromData()
 {
     $data = ['data'];
     $id = 33;
     $this->configurableAttribute->expects($this->any())->method('getData')->will($this->returnValue($data));
     $this->configurableAttribute->expects($this->once())->method('setData')->with($this->equalTo($data));
     $this->configurableAttribute->expects($this->once())->method('addData')->with($this->equalTo($data));
     $this->configurableAttribute->expects($this->any())->method('getId')->will($this->returnValue($id));
     $this->configurableAttribute->expects($this->once())->method('setId')->with($this->equalTo($id));
     $this->configurableAttribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($id));
     $this->configurableAttribute->expects($this->once())->method('setAttributeId')->with($this->equalTo($id));
     $this->configurableAttribute->expects($this->any())->method('getValues')->will($this->returnValue($data));
     $this->configurableAttribute->expects($this->any())->method('setValues')->with($this->equalTo($data));
     $this->option->expects($this->any())->method('getValues')->will($this->returnValue([$this->value]));
     $this->option->expects($this->any())->method('__toArray')->will($this->returnValue($data));
     $this->valueConverter->expects($this->any())->method('convertArrayFromData')->with($this->equalTo($this->value))->will($this->returnValue($data[0]));
     $result = $this->converter->getModelFromData($this->option, $this->configurableAttribute);
     $this->assertEquals($this->configurableAttribute, $result);
 }
コード例 #2
0
ファイル: WriteService.php プロジェクト: aiesh/magento2
 /**
  * {@inheritdoc}
  */
 public function add($productSku, Option $option)
 {
     $product = $this->productRepository->get($productSku);
     $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
     if (!in_array($product->getTypeId(), $allowedTypes)) {
         throw new \InvalidArgumentException('Incompatible product type');
     }
     $eavAttribute = $this->eavConfig->getAttribute(Product::ENTITY, $option->getAttributeId());
     /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
     $configurableAttribute = $this->configurableAttributeFactory->create();
     $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
     if ($configurableAttribute->getId()) {
         throw new CouldNotSaveException('Product already has this option');
     }
     try {
         $product->setTypeId(ConfigurableType::TYPE_CODE);
         $product->setConfigurableAttributesData([$this->optionConverter->convertArrayFromData($option)]);
         $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();
 }
コード例 #3
0
ファイル: OptionConverter.php プロジェクト: aiesh/magento2
 /**
  * @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;
 }