/**
  * Prepare variations
  *
  * @return void
  * @throws \Zend_Currency_Exception
  */
 protected function prepareVariations()
 {
     $variations = $this->getVariations();
     $productMatrix = [];
     $attributes = [];
     $productIds = [];
     if ($variations) {
         $usedProductAttributes = $this->getUsedAttributes();
         $productByUsedAttributes = $this->getAssociatedProducts();
         $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode());
         $configurableAttributes = $this->getAttributes();
         foreach ($variations as $variation) {
             $attributeValues = [];
             foreach ($usedProductAttributes as $attribute) {
                 $attributeValues[$attribute->getAttributeCode()] = $variation[$attribute->getId()]['value'];
             }
             $key = implode('-', $attributeValues);
             if (isset($productByUsedAttributes[$key])) {
                 $product = $productByUsedAttributes[$key];
                 $price = $product->getPrice();
                 $variationOptions = [];
                 foreach ($usedProductAttributes as $attribute) {
                     if (!isset($attributes[$attribute->getAttributeId()])) {
                         $attributes[$attribute->getAttributeId()] = ['code' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel(), 'id' => $attribute->getAttributeId(), 'position' => $configurableAttributes[$attribute->getAttributeId()]['position'], 'chosen' => []];
                         foreach ($attribute->getOptions() as $option) {
                             if (!empty($option->getValue())) {
                                 $attributes[$attribute->getAttributeId()]['options'][$option->getValue()] = ['attribute_code' => $attribute->getAttributeCode(), 'attribute_label' => $attribute->getStoreLabel(0), 'id' => $option->getValue(), 'label' => $option->getLabel(), 'value' => $option->getValue()];
                             }
                         }
                     }
                     $optionId = $variation[$attribute->getId()]['value'];
                     $variationOption = ['attribute_code' => $attribute->getAttributeCode(), 'attribute_label' => $attribute->getStoreLabel(0), 'id' => $optionId, 'label' => $variation[$attribute->getId()]['label'], 'value' => $optionId];
                     $variationOptions[] = $variationOption;
                     $attributes[$attribute->getAttributeId()]['chosen'][$optionId] = $variationOption;
                 }
                 $productMatrix[] = ['id' => $product->getId(), 'product_link' => '<a href="' . $this->urlBuilder->getUrl('catalog/product/edit', ['id' => $product->getId()]) . '" target="_blank">' . $product->getName() . '</a>', 'sku' => $product->getSku(), 'name' => $product->getName(), 'qty' => $this->getProductStockQty($product), 'price' => $currency->toCurrency(sprintf("%f", $price), ['display' => false]), 'price_string' => $currency->toCurrency(sprintf("%f", $price)), 'price_currency' => $this->locator->getStore()->getBaseCurrency()->getCurrencySymbol(), 'configurable_attribute' => $this->getJsonConfigurableAttributes($variationOptions), 'weight' => $product->getWeight(), 'status' => $product->getStatus(), 'variationKey' => $this->getVariationKey($variationOptions), 'canEdit' => 0, 'newProduct' => 0, 'attributes' => $this->getTextAttributes($variationOptions), 'thumbnail_image' => $this->imageHelper->init($product, 'product_thumbnail_image')->getUrl()];
                 $productIds[] = $product->getId();
             }
         }
     }
     $this->productMatrix = $productMatrix;
     $this->productIds = $productIds;
     $this->productAttributes = array_values($attributes);
 }
 /**
  * {@inheritdoc}
  */
 public function modifyData(array $data)
 {
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->locator->getProduct();
     $modelId = $product->getId();
     if ($modelId) {
         $storeId = $this->locator->getStore()->getId();
         /** @var \Magento\Framework\Currency $currency */
         $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode());
         $data[$product->getId()]['links'][self::LINK_TYPE] = [];
         foreach ($this->productLinkRepository->getList($product) as $linkItem) {
             if ($linkItem->getLinkType() !== self::LINK_TYPE) {
                 continue;
             }
             /** @var \Magento\Catalog\Api\Data\ProductInterface $linkedProduct */
             $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $storeId);
             $data[$modelId]['links'][self::LINK_TYPE][] = ['id' => $linkedProduct->getId(), 'name' => $linkedProduct->getName(), 'sku' => $linkItem->getLinkedProductSku(), 'price' => $currency->toCurrency(sprintf("%f", $linkedProduct->getPrice())), 'qty' => $linkItem->getExtensionAttributes()->getQty(), 'position' => $linkItem->getPosition(), 'thumbnail' => $this->imageHelper->init($linkedProduct, 'product_listing_thumbnail')->getUrl(), 'type_id' => $linkedProduct->getTypeId(), 'status' => $this->status->getOptionText($linkedProduct->getStatus()), 'attribute_set' => $this->attributeSetRepository->get($linkedProduct->getAttributeSetId())->getAttributeSetName()];
         }
         $data[$modelId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $storeId;
     }
     return $data;
 }
Exemple #3
0
 /**
  * Fill data column
  *
  * @param ProductInterface $linkedProduct
  * @param ProductLinkInterface $linkItem
  * @return array
  */
 protected function fillData(ProductInterface $linkedProduct, ProductLinkInterface $linkItem)
 {
     /** @var \Magento\Framework\Currency $currency */
     $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode());
     return ['id' => $linkedProduct->getId(), 'name' => $linkedProduct->getName(), 'sku' => $linkItem->getLinkedProductSku(), 'price' => $currency->toCurrency(sprintf("%f", $linkedProduct->getPrice())), 'qty' => $linkItem->getExtensionAttributes()->getQty(), 'position' => $linkItem->getPosition(), 'thumbnail' => $this->imageHelper->init($linkedProduct, 'product_listing_thumbnail')->getUrl(), 'type_id' => $linkedProduct->getTypeId(), 'status' => $this->status->getOptionText($linkedProduct->getStatus()), 'attribute_set' => $this->attributeSetRepository->get($linkedProduct->getAttributeSetId())->getAttributeSetName()];
 }