/**
  * Populate product with variation of attributes
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param array $attributes
  * @return \Magento\Catalog\Api\Data\ProductInterface[]
  */
 public function create(\Magento\Catalog\Api\Data\ProductInterface $product, $attributes)
 {
     $variations = $this->variationMatrix->getVariations($attributes);
     $products = [];
     foreach ($variations as $variation) {
         $price = $product->getPrice();
         /** @var \Magento\Catalog\Model\Product $item */
         $item = $this->productFactory->create();
         $item->setData($product->getData());
         $suffix = '';
         foreach ($variation as $attributeId => $valueInfo) {
             $suffix .= '-' . $valueInfo['value'];
             $customAttribute = $this->customAttributeFactory->create()->setAttributeCode($attributes[$attributeId]['attribute_code'])->setValue($valueInfo['value']);
             $customAttributes = array_merge($item->getCustomAttributes(), [$attributes[$attributeId]['attribute_code'] => $customAttribute]);
             $item->setData('custom_attributes', $customAttributes);
             $priceInfo = $valueInfo['price'];
             $price += (!empty($priceInfo['is_percent']) ? $product->getPrice() / 100.0 : 1.0) * $priceInfo['pricing_value'];
         }
         $item->setPrice($price);
         $item->setName($product->getName() . $suffix);
         $item->setSku($product->getSku() . $suffix);
         $item->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE);
         $products[] = $item;
     }
     return $products;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function generateVariation(Product $product, $options)
 {
     $attributes = $this->getAttributesForMatrix($options);
     $variations = $this->variationMatrix->getVariations($attributes);
     $products = $this->populateProductVariation($product, $variations, $attributes);
     return $products;
 }
Example #3
0
 /**
  * @param array $data
  * @return array
  */
 protected function getVariationsMatrix($data)
 {
     $variations = $this->variationMatrix->getVariations($data['configurable_attributes_data']);
     $result = [];
     $productPrice = 100;
     $productName = $data['name'];
     $productSku = $data['sku'];
     foreach ($variations as $variation) {
         $attributeValues = [];
         $attributeLabels = [];
         $price = $productPrice;
         foreach ($data['configurable_attributes_data'] as $attributeData) {
             $attributeId = $attributeData['attribute_id'];
             $attributeValues[$attributeData['attribute_code']] = $variation[$attributeId]['value'];
             $attributeLabels[$attributeData['attribute_code']] = $variation[$attributeId]['label'];
             if (isset($variation[$attributeId]['price'])) {
                 $priceInfo = $variation[$attributeId]['price'];
                 $price += ($priceInfo['is_percent'] ? $productPrice / 100.0 : 1.0) * $priceInfo['pricing_value'];
             }
         }
         $key = implode('-', $attributeValues);
         $result[$key] = ['image' => '', 'name' => $productName . '-' . implode('-', $attributeLabels), 'sku' => $productSku . '-' . implode('-', $attributeLabels), 'configurable_attribute' => \json_encode($attributeValues), 'quantity_and_stock_status' => ['qty' => '10'], 'weight' => '1'];
     }
     return $result;
 }
Example #4
0
 /**
  * @param array $configurableAttributeData
  * @dataProvider productVariationDataProvider
  */
 public function testGenerateVariation($configurableAttributeData)
 {
     $attributeCode = 'code';
     $attribute = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Eav\\AttributeMetadata')->disableOriginalConstructor()->getMock();
     $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode));
     $this->attributeReadService->expects($this->once())->method('info')->with($configurableAttributeData['attribute_id'])->will($this->returnValue($attribute));
     $options = null;
     $this->variationMatrix->expects($this->any())->method('getVariations')->with([$configurableAttributeData['attribute_id'] => ["attribute_id" => $configurableAttributeData['attribute_id'], "values" => $configurableAttributeData['values'], "options" => $options, "attribute_code" => $attributeCode]])->will($this->returnValue([[$configurableAttributeData['attribute_id'] => ['value' => '14', 'label' => 'dd', 'price' => ['index' => 14, 'price' => 10]]]]));
     $product = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Product')->disableOriginalConstructor()->getMock();
     $product->expects($this->any())->method('getPrice')->will($this->returnValue(100));
     $configurableAttribute = $this->getMockBuilder('Magento\\ConfigurableProduct\\Service\\V1\\Data\\Option')->disableOriginalConstructor()->getMock();
     $configurableAttribute->expects($this->any())->method('__toArray')->will($this->returnValue($configurableAttributeData));
     $configurableAttribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($configurableAttributeData['attribute_id']));
     $this->productBuilder->expects($this->any())->method('populute')->with($product);
     $this->productBuilder->expects($this->once())->method('setCustomAttribute')->with($attributeCode, 14);
     $this->productBuilder->expects($this->once())->method('setPrice')->with(110);
     $this->productBuilder->expects($this->once())->method('create')->will($this->returnValue($product));
     $result = $this->object->generateVariation($product, [$configurableAttribute]);
     $this->assertCount(1, $result);
     $this->assertEquals([$product], $result);
 }
Example #5
0
 /**
  * Retrieve all possible attribute values combinations
  *
  * @return array
  */
 public function getVariations()
 {
     return $this->variationMatrix->getVariations($this->getAttributes());
 }
 public function testGetVariations()
 {
     $result = [[130 => ['value' => '3', 'label' => 'red', 'price' => ['value_index' => '3', 'pricing_value' => '', 'is_percent' => '0', 'include' => '1']]], [130 => ['value' => '4', 'label' => 'blue', 'price' => ['value_index' => '4', 'pricing_value' => '', 'is_percent' => '0', 'include' => '1']]]];
     $input = [130 => ['values' => [['value_index' => '3', 'pricing_value' => '', 'is_percent' => '0', 'include' => '1'], ['value_index' => '4', 'pricing_value' => '', 'is_percent' => '0', 'include' => '1']], 'attribute_id' => '130', 'options' => [['value' => '3', 'label' => 'red'], ['value' => '4', 'label' => 'blue']]]];
     $this->assertEquals($result, $this->model->getVariations($input));
 }