/**
  * 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 4
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));
 }