/**
  * @return void
  */
 public function testModifyMeta()
 {
     $this->locatorMock->expects($this->exactly(2))->method('getProduct')->willReturn($this->productMock);
     $this->productMock->expects($this->any())->method('getTypeId');
     $this->arrayManagerMock->expects($this->exactly(3))->method('set')->willReturn([]);
     $this->assertEquals([], $this->downloadablePanel->modifyMeta([]));
 }
 public function testAfterLoadWithExistingExtensionAttributes()
 {
     // test when extension attributes already exist
     $this->productMock->expects($this->once())->method('getExtensionAttributes')->willReturn($this->productExtensionMock);
     $this->productExtensionFactoryMock->expects($this->never())->method('create');
     $this->assertEquals($this->productMock, $this->plugin->afterLoad($this->productMock));
 }
Example #3
0
 protected function setUp()
 {
     $this->objectManager = new ObjectManager($this);
     $this->locatorMock = $this->getMockBuilder(LocatorInterface::class)->getMockForAbstractClass();
     $this->productMock = $this->getMockBuilder(ProductInterface::class)->setMethods(['getId', 'getTypeId'])->getMockForAbstractClass();
     $this->productMock->expects($this->any())->method('getId')->willReturn(self::PRODUCT_ID);
     $this->productMock->expects($this->any())->method('getTypeId')->willReturn(GroupedProductType::TYPE_CODE);
     $this->linkedProductMock = $this->getMockBuilder(ProductInterface::class)->setMethods(['getId', 'getName', 'getPrice'])->getMockForAbstractClass();
     $this->linkedProductMock->expects($this->any())->method('getId')->willReturn(self::LINKED_PRODUCT_ID);
     $this->linkedProductMock->expects($this->any())->method('getName')->willReturn(self::LINKED_PRODUCT_NAME);
     $this->linkedProductMock->expects($this->any())->method('getPrice')->willReturn(self::LINKED_PRODUCT_PRICE);
     $this->linkMock = $this->getMockBuilder(ProductLinkInterface::class)->setMethods(['getLinkType', 'getLinkedProductSku', 'getPosition', 'getExtensionAttributes'])->getMockForAbstractClass();
     $this->linkExtensionMock = $this->getMockBuilder(ProductLinkExtensionInterface::class)->setMethods(['getQty'])->getMockForAbstractClass();
     $this->linkExtensionMock->expects($this->any())->method('getQty')->willReturn(self::LINKED_PRODUCT_QTY);
     $this->linkMock->expects($this->any())->method('getExtensionAttributes')->willReturn($this->linkExtensionMock);
     $this->linkMock->expects($this->any())->method('getPosition')->willReturn(self::LINKED_PRODUCT_POSITION);
     $this->linkMock->expects($this->any())->method('getLinkedProductSku')->willReturn(self::LINKED_PRODUCT_SKU);
     $this->linkMock->expects($this->any())->method('getLinkType')->willReturn(Grouped::LINK_TYPE);
     $this->linkRepositoryMock = $this->getMockBuilder(ProductLinkRepositoryInterface::class)->setMethods(['getList'])->getMockForAbstractClass();
     $this->linkRepositoryMock->expects($this->any())->method('getList')->with($this->productMock)->willReturn([$this->linkMock]);
     $this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)->setMethods(['get'])->getMockForAbstractClass();
     $this->productRepositoryMock->expects($this->any())->method('get')->with(self::LINKED_PRODUCT_SKU)->willReturn($this->linkedProductMock);
     $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['getId'])->getMockForAbstractClass();
     $this->locatorMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
     $this->locatorMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
 }
Example #4
0
 /**
  * Retrieve backend model of product media gallery attribute
  *
  * @param Product $product
  * @return \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  * @throws StateException
  */
 protected function getGalleryAttributeBackend(Product $product)
 {
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     if ($galleryAttributeBackend == null) {
         throw new StateException(__('Requested product does not support images.'));
     }
     return $galleryAttributeBackend;
 }
 /**
  * Get linked to configurable simple products
  *
  * @param ProductInterface $product
  * @return int[]
  */
 private function getLinkedProducts(ProductInterface $product)
 {
     /** @var Configurable $typeInstance */
     $typeInstance = $product->getTypeInstance();
     $childrenIds = $typeInstance->getChildrenIds($product->getId());
     if (isset($childrenIds[0])) {
         return $childrenIds[0];
     } else {
         return [];
     }
 }
 /**
  * Add Product Filter to Collection
  *
  * @param int|array|ProductInterface $product
  * @return $this
  */
 public function addProductFilter($product)
 {
     $id = -1;
     if ($product instanceof ProductInterface) {
         $id = $product->getId();
     } else {
         if (is_numeric($product)) {
             $id = $product;
         }
     }
     $this->addFieldToFilter('product', $id);
     return $this;
 }
Example #7
0
 /**
  * Retrieve collection of gallery images
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @return \Magento\Catalog\Model\Product\Image[]|null
  */
 public function getGalleryImages(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     $images = $product->getMediaGalleryImages();
     if ($images instanceof \Magento\Framework\Data\Collection) {
         foreach ($images as $image) {
             /** @var $image \Magento\Catalog\Model\Product\Image */
             $image->setData('small_image_url', $this->imageHelper->init($product, 'product_page_image_small')->setImageFile($image->getFile())->getUrl());
             $image->setData('medium_image_url', $this->imageHelper->init($product, 'product_page_image_medium')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl());
             $image->setData('large_image_url', $this->imageHelper->init($product, 'product_page_image_large')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl());
         }
     }
     return $images;
 }
Example #8
0
 public function testDisallowModifyMeta()
 {
     $meta = ['some meta'];
     $modifiers = ['modifier1', 'modifier2'];
     $this->productMock->expects(static::any())->method('getTypeId')->willReturn(ConfigurableType::TYPE_CODE);
     $this->allowedProductTypesMock->expects(static::once())->method('isAllowedProductType')->with($this->productMock)->willReturn(false);
     $this->objectManagerMock->expects(static::never())->method('get');
     $this->assertSame($meta, $this->createCompositeModifier($modifiers)->modifyMeta($meta));
 }
 /**
  * @return void
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Type "SomeClass" is not an instance of
  * Magento\Ui\DataProvider\Modifier\ModifierInterface
  */
 public function testModifyMetaWithException()
 {
     /** @var \Exception|MockObject $modifierMock */
     $modifierMock = $this->getMock(\Exception::class);
     $modifierMock->expects($this->never())->method('modifyMeta');
     $this->productMock->expects($this->once())->method('getTypeId')->willReturn(Type::TYPE_CODE);
     $this->objectManagerMock->expects($this->once())->method('get')->with($this->modifierClass)->willReturn($modifierMock);
     $this->composite->modifyMeta($this->meta);
 }
 /**
  * Return the stock item that needs to be updated.
  * If the stock item does not need to be updated, return null.
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @return \Magento\CatalogInventory\Api\Data\StockItemInterface|null
  */
 protected function getStockItemToBeUpdated($product)
 {
     // from the API, all the data we care about will exist as extension attributes of the original product
     $extendedAttributes = $product->getExtensionAttributes();
     if ($extendedAttributes !== null) {
         $stockItem = $extendedAttributes->getStockItem();
         if ($stockItem != null) {
             return $stockItem;
         }
     }
     // we have no new stock item information to update, however we need to ensure that the product does have some
     // stock item information present.  On a newly created product, it will not have any stock item info.
     $stockItem = $this->stockRegistry->getStockItem($product->getId());
     if ($stockItem->getItemId() != null) {
         // we already have stock item info, so we return null since nothing more needs to be updated
         return null;
     }
     return $stockItem;
 }
Example #11
0
 /**
  * @param int|null $id
  * @param string $typeId
  * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expectedGetTitle
  * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expectedGetValue
  * @return void
  * @dataProvider getLinksTitleDataProvider
  */
 public function testGetLinksTitle($id, $typeId, $expectedGetTitle, $expectedGetValue)
 {
     $title = 'My Title';
     $this->locatorMock->expects($this->any())->method('getProduct')->willReturn($this->productMock);
     $this->productMock->expects($this->once())->method('getId')->willReturn($id);
     $this->productMock->expects($this->any())->method('getTypeId')->willReturn($typeId);
     $this->productMock->expects($expectedGetTitle)->method('getLinksTitle')->willReturn($title);
     $this->scopeConfigMock->expects($expectedGetValue)->method('getValue')->willReturn($title);
     $this->assertEquals($title, $this->links->getLinksTitle());
 }
 /**
  * @return void
  */
 public function testModifyMeta()
 {
     $this->locatorMock->expects($this->once())->method('getProduct')->willReturn($this->productMock);
     $this->productMock->expects($this->once())->method('getTypeId');
     $this->storeManagerMock->expects($this->once())->method('isSingleStoreMode');
     $this->typeUploadMock->expects($this->once())->method('toOptionArray');
     $this->urlBuilderMock->expects($this->once())->method('addSessionParam')->willReturnSelf();
     $this->urlBuilderMock->expects($this->once())->method('getUrl');
     $this->arrayManagerMock->expects($this->exactly(6))->method('set')->willReturn([]);
     $this->assertEquals([], $this->samples->modifyMeta([]));
 }
 /**
  * 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 #14
0
 /**
  * @param ProductInterface $product
  * @return OptionInterface[]
  */
 public function load(ProductInterface $product)
 {
     $options = [];
     /** @var Configurable $typeInstance */
     $typeInstance = $product->getTypeInstance();
     $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product);
     $this->extensionAttributesJoinProcessor->process($attributeCollection);
     foreach ($attributeCollection as $attribute) {
         $values = [];
         $attributeOptions = $attribute->getOptions();
         if (is_array($attributeOptions)) {
             foreach ($attributeOptions as $option) {
                 /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */
                 $value = $this->optionValueFactory->create();
                 $value->setValueIndex($option['value_index']);
                 $values[] = $value;
             }
         }
         $attribute->setValues($values);
         $options[] = $attribute;
     }
     return $options;
 }
Example #15
0
 /**
  * Retrieve websites
  *
  * @param ProductInterface $product
  * @param EavAttribute $eavAttribute
  * @return array
  */
 public function getWebsites(ProductInterface $product, EavAttribute $eavAttribute)
 {
     if (null !== $this->websites) {
         return $this->websites;
     }
     $websites = [['value' => 0, 'label' => $this->formatLabel(__('All Websites'), $this->directoryHelper->getBaseCurrencyCode())]];
     if ($this->storeManager->hasSingleStore() || $eavAttribute->getEntityAttribute() && $eavAttribute->getEntityAttribute()->isScopeGlobal()) {
         return $this->websites = $websites;
     }
     if ($storeId = $this->locator->getStore()->getId()) {
         /** @var WebsiteInterface $website */
         $website = $this->storeManager->getStore($storeId)->getWebsite();
         $websites[$website->getId()] = ['value' => $website->getId(), 'label' => $this->formatLabel($website->getName(), $website->getConfig(Currency::XML_PATH_CURRENCY_BASE))];
     } else {
         /** @var WebsiteInterface $website */
         foreach ($this->storeManager->getWebsites() as $website) {
             if (!in_array($website->getId(), $product->getWebsiteIds())) {
                 continue;
             }
             $websites[$website->getId()] = ['value' => $website->getId(), 'label' => $this->formatLabel($website->getName(), $website->getConfig(Currency::XML_PATH_CURRENCY_BASE))];
         }
     }
     return $this->websites = $websites;
 }
 /**
  * @return void
  */
 public function testModifyMeta()
 {
     $this->locatorMock->expects($this->once())->method('getProduct')->willReturn($this->productMock);
     $this->productMock->expects($this->any())->method('getTypeId');
     $this->storeManagerMock->expects($this->exactly(2))->method('isSingleStoreMode');
     $this->typeUploadMock->expects($this->exactly(2))->method('toOptionArray');
     $this->shareableMock->expects($this->once())->method('toOptionArray');
     $this->urlBuilderMock->expects($this->exactly(2))->method('addSessionParam')->willReturnSelf();
     $this->urlBuilderMock->expects($this->exactly(2))->method('getUrl');
     $currencyMock = $this->getMock(\Magento\Directory\Model\Currency::class, [], [], '', false);
     $currencyMock->expects($this->once())->method('getCurrencySymbol');
     $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->setMethods(['getBaseCurrency'])->getMockForAbstractClass();
     $storeMock->expects($this->once())->method('getBaseCurrency')->willReturn($currencyMock);
     $this->locatorMock->expects($this->once())->method('getStore')->willReturn($storeMock);
     $this->arrayManagerMock->expects($this->exactly(9))->method('set')->willReturn([]);
     $this->assertEquals([], $this->links->modifyMeta([]));
 }
 /**
  * {@inheritdoc}
  */
 public function getProducts(ProductInterface $product)
 {
     if (!isset($this->products[$product->getId()])) {
         if ($this->requestSafety->isSafeMethod()) {
             $productIds = $this->resource->getConnection()->fetchCol('(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')');
             $this->products[$product->getId()] = $this->collectionFactory->create()->addAttributeToSelect(['price', 'special_price'])->addIdFilter($productIds);
         } else {
             $this->products[$product->getId()] = $this->configurable->getUsedProducts($product);
         }
     }
     return $this->products[$product->getId()];
 }
 /**
  * @expectedException \Magento\Framework\Exception\LocalizedException
  * @expectedExceptionMessage Invalid stock item id: 35. Assigned stock item id is 40
  */
 public function testAroundSaveWithNotAssignedStockItemId()
 {
     $stockId = 80;
     $stockItemId = 35;
     $defaultScopeId = 100;
     $defaultStockId = 80;
     $storedStockitemId = 40;
     $this->stockItem->expects($this->once())->method('getStockId')->willReturn($stockId);
     $this->stockRegistry->expects($this->once())->method('getStock')->with($defaultScopeId)->willReturn($this->defaultStock);
     $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId')->willReturn($defaultScopeId);
     $this->defaultStock->expects($this->once())->method('getStockId')->willReturn($defaultStockId);
     $this->product->expects($this->once())->method('getExtensionAttributes')->willReturn($this->productExtension);
     $this->productExtension->expects($this->once())->method('getStockItem')->willReturn($this->stockItem);
     $this->stockItem->expects($this->once())->method('getItemId')->willReturn($stockItemId);
     $storedStockItem = $this->getMockBuilder(StockItemInterface::class)->setMethods(['getItemId'])->getMockForAbstractClass();
     $storedStockItem->expects($this->once())->method('getItemId')->willReturn($storedStockitemId);
     $this->stockRegistry->expects($this->once())->method('getStockItem')->willReturn($storedStockItem);
     $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product);
 }
 public function testAroundSave()
 {
     $productId = 5494;
     $websiteId = 1;
     $storeId = 2;
     $sku = 'my product that needs saving';
     $this->productMock->expects($this->once())->method('getExtensionAttributes')->willReturn($this->productExtensionMock);
     $this->productExtensionMock->expects($this->once())->method('getStockItem')->willReturn($this->stockItemMock);
     $storeMock = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock();
     $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
     $this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($storeMock);
     $this->savedProductMock->expects($this->once())->method('getId')->willReturn($productId);
     $this->savedProductMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId);
     $this->savedProductMock->expects($this->atLeastOnce())->method('getSku')->willReturn($sku);
     $this->stockItemMock->expects($this->once())->method('setProductId')->with($productId);
     $this->stockItemMock->expects($this->once())->method('setWebsiteId')->with($websiteId);
     $this->stockRegistry->expects($this->once())->method('updateStockItemBySku')->with($sku, $this->stockItemMock);
     $newProductMock = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductInterface')->disableOriginalConstructor()->getMock();
     $this->productRepositoryMock->expects($this->once())->method('get')->with($sku, false, $storeId, true)->willReturn($newProductMock);
     $this->assertEquals($newProductMock, $this->plugin->aroundSave($this->productRepositoryMock, $this->closureMock, $this->productMock));
 }
 /**
  * Initialize data for configurable product
  *
  * @param Helper $subject
  * @param ProductInterface $product
  * @return ProductInterface
  * @throws \InvalidArgumentException
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterInitialize(Helper $subject, ProductInterface $product)
 {
     $attributes = $this->request->getParam('attributes');
     $productData = $this->request->getPost('product', []);
     if ($product->getTypeId() !== ConfigurableProduct::TYPE_CODE || empty($attributes)) {
         return $product;
     }
     $setId = $this->request->getPost('new-variations-attribute-set-id');
     if ($setId) {
         $product->setAttributeSetId($setId);
     }
     $extensionAttributes = $product->getExtensionAttributes();
     $product->setNewVariationsAttributeSetId($setId);
     $configurableOptions = [];
     if (!empty($productData['configurable_attributes_data'])) {
         $configurableOptions = $this->optionsFactory->create((array) $productData['configurable_attributes_data']);
     }
     $extensionAttributes->setConfigurableProductOptions($configurableOptions);
     $this->setLinkedProducts($product, $extensionAttributes);
     $product->setCanSaveConfigurableAttributes((bool) $this->request->getPost('affect_configurable_product_attributes'));
     $product->setExtensionAttributes($extensionAttributes);
     return $product;
 }
 /**
  * Save related products
  *
  * @param ProductInterface $product
  * @return void
  * @deprecated
  */
 private function saveRelatedProducts(ProductInterface $product)
 {
     $productIds = $product->getAssociatedProductIds();
     if (is_array($productIds)) {
         $this->typeConfigurableFactory->create()->saveProducts($product, $productIds);
     }
 }
Example #22
0
 /**
  * {@inheritdoc}
  */
 public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     $sku = $product->getSku();
     $productId = $product->getId();
     try {
         $this->resourceModel->delete($product);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\StateException(__('Unable to remove product %1', $sku));
     }
     unset($this->instances[$sku]);
     unset($this->instancesById[$productId]);
     return true;
 }
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @return \Magento\Bundle\Api\Data\OptionTypeInterface[]
  */
 private function getOptions(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     /** @var \Magento\Bundle\Model\Product\Type $productTypeInstance */
     $productTypeInstance = $product->getTypeInstance();
     $productTypeInstance->setStoreFilter($product->getStoreId(), $product);
     $optionCollection = $productTypeInstance->getOptionsCollection($product);
     $selectionCollection = $productTypeInstance->getSelectionsCollection($productTypeInstance->getOptionsIds($product), $product);
     $options = $optionCollection->appendSelections($selectionCollection);
     return $options;
 }
Example #24
0
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param LinkInterface $link
  * @param bool $isGlobalScopeContent
  * @return mixed
  * @throws InputException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function updateLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent)
 {
     /** @var $existingLink \Magento\Downloadable\Model\Link */
     $existingLink = $this->linkFactory->create()->load($link->getId());
     if (!$existingLink->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable link with provided ID.'));
     }
     if ($existingLink->getProductId() != $product->getId()) {
         throw new InputException(__('Provided downloadable link is not related to given product.'));
     }
     $validateLinkContent = $link->getLinkFileContent() === null ? false : true;
     $validateSampleContent = $link->getSampleFileContent() === null ? false : true;
     if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
         throw new InputException(__('Provided link information is invalid.'));
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $link->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException(__('Link title cannot be empty.'));
         }
     }
     if ($link->getLinkType() == 'file' && $link->getLinkFileContent() === null) {
         $link->setLinkFile($existingLink->getLinkFile());
     }
     if ($link->getSampleType() == 'file' && $link->getSampleFileContent() === null) {
         $link->setSampleFile($existingLink->getSampleFile());
     }
     $this->saveLink($product, $link, $isGlobalScopeContent);
     return $existingLink->getId();
 }
 /**
  * @param ProductInterface $product
  * @param StockItemInterface $stockItem
  * @throws LocalizedException
  * @return void
  */
 private function validateStockItem(ProductInterface $product, StockItemInterface $stockItem)
 {
     $defaultScopeId = $this->stockConfiguration->getDefaultScopeId();
     $defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId();
     $stockId = $stockItem->getStockId();
     if ($stockId !== null && $stockId != $defaultStockId) {
         throw new LocalizedException(__('Invalid stock id: %1. Only default stock with id %2 allowed', $stockId, $defaultStockId));
     }
     $stockItemId = $stockItem->getItemId();
     if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) {
         throw new LocalizedException(__('Invalid stock item id: %1. Should be null or numeric value greater than 0', $stockItemId));
     }
     $defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId();
     if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) {
         throw new LocalizedException(__('Invalid stock item id: %1. Assigned stock item id is %2', $stockItemId, $defaultStockItemId));
     }
 }
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Downloadable\Api\Data\SampleInterface[] $samples
  * @return $this
  */
 protected function saveSamples(\Magento\Catalog\Api\Data\ProductInterface $product, array $samples)
 {
     $existingSampleIds = [];
     $extensionAttributes = $product->getExtensionAttributes();
     if ($extensionAttributes !== null) {
         $existingSamples = $extensionAttributes->getDownloadableProductSamples();
         if ($existingSamples !== null) {
             foreach ($existingSamples as $existingSample) {
                 $existingSampleIds[] = $existingSample->getId();
             }
         }
     }
     $updatedSampleIds = [];
     foreach ($samples as $sample) {
         $sampleId = $sample->getId();
         if ($sampleId) {
             $updatedSampleIds[] = $sampleId;
         }
         $this->sampleRepository->save($product->getSku(), $sample);
     }
     $sampleIdsToDelete = array_diff($existingSampleIds, $updatedSampleIds);
     foreach ($sampleIdsToDelete as $sampleId) {
         $this->sampleRepository->delete($sampleId);
     }
     return $this;
 }
Example #27
0
 /**
  * Get variation-matrix from product
  *
  * @param ProductInterface $product
  * @return array
  */
 private function getVariationMatrixFromProduct(ProductInterface $product)
 {
     $result = [];
     $configurableMatrix = $product->hasData('configurable-matrix') ? $product->getData('configurable-matrix') : [];
     foreach ($configurableMatrix as $item) {
         if ($item['newProduct']) {
             $result[$item['variationKey']] = $this->mapData($item);
             if (isset($item['qty'])) {
                 $result[$item['variationKey']]['quantity_and_stock_status']['qty'] = $item['qty'];
             }
         }
     }
     return $result;
 }
 /**
  * Set the project associated with this text8
  * @param \Magento\Catalog\Api\Data\ProductInterface $productInterface
  * @return \Tudock\HelloWorld\Api\Data\HelloTextInterface
  */
 public function setProduct(\Magento\Catalog\Api\Data\ProductInterface $productInterface)
 {
     $this->setData('product', $productInterface->getId());
     return $this;
 }
Example #29
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()];
 }
Example #30
0
 /**
  * Remove product attributes
  *
  * @param ProductInterface $product
  * @return void
  */
 private function deleteConfigurableProductAttributes(ProductInterface $product)
 {
     $list = $this->optionRepository->getList($product->getSku());
     foreach ($list as $item) {
         $this->optionRepository->deleteById($product->getSku(), $item->getId());
     }
 }