public function testCreateCreatesProductInEditMode()
 {
     $this->productMock->expects($this->once())->method('getIdBySku')->with('test_sku')->will($this->returnValue('test_id'));
     $this->productMock->expects($this->once())->method('setData')->with('_edit_mode', true);
     $this->productMock->expects($this->once())->method('load')->with('test_id');
     $this->assertSame($this->productMock, $this->model->get('test_sku', true));
 }
 /**
  * {@inheritdoc}
  */
 public function load($id, \Magento\Catalog\Service\V1\Data\ProductBuilder $productBuilder)
 {
     /** @var \Magento\Catalog\Model\Product */
     $product = $this->productRepository->get($id);
     if ($product->getTypeId() != ProductType::TYPE_BUNDLE) {
         return;
     }
     $productBuilder->setCustomAttribute('bundle_product_options', $this->optionReadService->getList($product->getSku()));
 }
示例#3
0
 /**
  * @return array
  */
 protected function prepareProducts($price)
 {
     $this->product = $this->productRepository->get('simple');
     $productSecond = clone $this->product;
     $productSecond->setId(null)->setUrlKey(null)->setSku(uniqid($this->product->getSku() . '-'))->setName(uniqid($this->product->getName() . '-'))->setWebsiteIds([1]);
     $productSecond->save();
     $productSecond->setPrice($price)->save();
     $productThird = clone $this->product;
     $productThird->setId(null)->setUrlKey(null)->setSku(uniqid($this->product->getSku() . '-'))->setName(uniqid($this->product->getName() . '-'))->setWebsiteIds([1])->save();
     $productThird->setPrice($price)->save();
     return [$productSecond->getEntityId(), $productThird->getEntityId()];
 }
示例#4
0
 /**
  * @inheritdoc
  */
 public function getChildren($productId)
 {
     $product = $this->productRepository->get($productId);
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new Exception('Only implemented for bundle product', Exception::HTTP_FORBIDDEN);
     }
     $childrenList = [];
     foreach ($this->getOptions($product) as $option) {
         /** @var \Magento\Catalog\Model\Product $selection */
         foreach ($option->getSelections() as $selection) {
             $childrenList[] = $this->linkConverter->createDataFromModel($selection, $product);
         }
     }
     return $childrenList;
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function getChildren($productId)
 {
     $product = $this->productRepository->get($productId);
     if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
         return [];
     }
     $childrenList = [];
     /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
     $productTypeInstance = $product->getTypeInstance();
     $productTypeInstance->setStoreFilter($product->getStoreId(), $product);
     foreach ($productTypeInstance->getUsedProducts($product) as $child) {
         $childrenList[] = $this->productConverter->createProductDataFromModel($child);
     }
     return $childrenList;
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function update($productSku, $sampleId, DownloadableSampleContent $sampleContent, $isGlobalScopeContent = false)
 {
     $product = $this->productRepository->get($productSku, true);
     /** @var $sample \Magento\Downloadable\Model\Sample */
     $sample = $this->sampleFactory->create()->load($sampleId);
     if (!$sample->getId()) {
         throw new NoSuchEntityException('There is no downloadable sample with provided ID.');
     }
     if ($sample->getProductId() != $product->getId()) {
         throw new InputException('Provided downloadable sample is not related to given product.');
     }
     if (!$this->contentValidator->isValid($sampleContent)) {
         throw new InputException('Provided sample information is invalid.');
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $sampleContent->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException('Sample title cannot be empty.');
         }
         // use title from GLOBAL scope
         $sample->setTitle(null);
     } else {
         $sample->setTitle($sampleContent->getTitle());
     }
     $sample->setProductId($product->getId())->setStoreId($product->getStoreId())->setSortOrder($sampleContent->getSortOrder())->save();
     return true;
 }
示例#7
0
 /**
  * {@inheritdoc}
  */
 public function update($productSku, $linkId, DownloadableLinkContent $linkContent, $isGlobalScopeContent = false)
 {
     $product = $this->productRepository->get($productSku, true);
     /** @var $link \Magento\Downloadable\Model\Link */
     $link = $this->linkFactory->create()->load($linkId);
     if (!$link->getId()) {
         throw new NoSuchEntityException('There is no downloadable link with provided ID.');
     }
     if ($link->getProductId() != $product->getId()) {
         throw new InputException('Provided downloadable link is not related to given product.');
     }
     if (!$this->linkContentValidator->isValid($linkContent)) {
         throw new InputException('Provided link information is invalid.');
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $linkContent->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException('Link title cannot be empty.');
         }
         // use title from GLOBAL scope
         $link->setTitle(null);
     } else {
         $link->setTitle($linkContent->getTitle());
     }
     $link->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds())->setSortOrder($linkContent->getSortOrder())->setPrice($linkContent->getPrice())->setIsShareable($linkContent->isShareable())->setNumberOfDownloads($linkContent->getNumberOfDownloads())->save();
     return true;
 }
示例#8
0
 /**
  * @inheritdoc
  */
 public function removeChild($productSku, $optionId, $childSku)
 {
     $product = $this->productRepository->get($productSku);
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new Exception(sprintf('Product with specified sku: %s is not a bundle product', $productSku), Exception::HTTP_FORBIDDEN);
     }
     $excludeSelectionIds = array();
     $usedProductIds = array();
     $removeSelectionIds = array();
     foreach ($this->getOptions($product) as $option) {
         foreach ($option->getSelections() as $selection) {
             if (strcasecmp($selection->getSku(), $childSku) == 0 && $selection->getOptionId() == $optionId) {
                 $removeSelectionIds[] = $selection->getSelectionId();
                 continue;
             }
             $excludeSelectionIds[] = $selection->getSelectionId();
             $usedProductIds[] = $selection->getProductId();
         }
     }
     if (empty($removeSelectionIds)) {
         throw new NoSuchEntityException('Requested bundle option product doesn\'t exist');
     }
     /* @var $resource \Magento\Bundle\Model\Resource\Bundle */
     $resource = $this->bundleFactory->create();
     $resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds);
     $resource->saveProductRelations($product->getId(), array_unique($usedProductIds));
     return true;
 }
 /**
  * @magentoDataFixture Magento/Bundle/_files/product.php
  * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
  * @magentoAppIsolation enabled
  */
 public function testUpdateBundleProduct()
 {
     // get existing bundle product
     $savedProduct = $this->productService->get('bundle-product');
     /** @var Link $newLink */
     $newLink = $this->linkBuilder->setSku('simple2')->create();
     /** @var Link[] $links */
     $links = array($newLink);
     /** @var Option $newOption */
     $newOption = $this->optionBuilder->setProductLinks($links)->create();
     /** @var Product bundleProduct */
     $updatedBundleProduct = $this->productBuilder->populate($savedProduct)->setCustomAttribute('bundle_product_options', array($newOption))->setCustomAttribute('price_view', 'test')->setCustomAttribute('price', 10)->create();
     $this->assertEquals('bundle-product', $this->productService->update('bundle-product', $updatedBundleProduct));
     $this->productRepository->get('bundle-product')->unsetData('_cache_instance_options_collection');
     // load and confirm number of links and options
     $savedProduct = $this->productService->get('bundle-product');
     /** @var Option[] $updatedOptions */
     $savedOptions = $savedProduct->getCustomAttribute('bundle_product_options')->getValue();
     $this->assertTrue(is_array($savedOptions));
     $this->assertEquals(1, count($savedOptions));
     $option = $savedOptions[0];
     $linkedProducts = $option->getProductLinks();
     $this->assertTrue(is_array($linkedProducts));
     $this->assertEquals(1, count($linkedProducts));
     $link = $linkedProducts[0];
     $this->assertEquals('simple2', $link->getSku());
 }
示例#10
0
 /**
  * @param string $productSku
  * @return Product
  * @throws Exception
  */
 private function getProduct($productSku)
 {
     $product = $this->productRepository->get($productSku);
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new Exception('Product with specified sku: "%1" is not a bundle product', Exception::HTTP_FORBIDDEN, Exception::HTTP_FORBIDDEN, [$product->getSku()]);
     }
     return $product;
 }
示例#11
0
 /**
  * @param string $productSku
  * @return \Magento\Catalog\Model\Product
  * @throws \Magento\Webapi\Exception
  */
 private function getProduct($productSku)
 {
     $product = $this->productRepository->get($productSku);
     if (ConfigurableType::TYPE_CODE !== $product->getTypeId()) {
         throw new Exception('Product with specified sku: "%1" is not a configurable product', Exception::HTTP_FORBIDDEN, Exception::HTTP_FORBIDDEN, [$product->getSku()]);
     }
     return $product;
 }
示例#12
0
 /**
  * @param string $productSku
  * @return Product
  * @throws Exception
  */
 private function getProduct($productSku)
 {
     $product = $this->productRepository->get($productSku);
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new Exception('Only implemented for bundle product', Exception::HTTP_FORBIDDEN);
     }
     return $product;
 }
示例#13
0
 /**
  * Retrieve product instance by sku
  *
  * @param string $productSku
  * @return \Magento\Catalog\Model\Product
  * @throws \Magento\Webapi\Exception
  */
 private function getProduct($productSku)
 {
     $product = $this->productRepository->get($productSku);
     if (ConfigurableType::TYPE_CODE !== $product->getTypeId()) {
         throw new Exception(sprintf('Only implemented for configurable product: %s', $productSku), Exception::HTTP_FORBIDDEN);
     }
     return $product;
 }
示例#14
0
 /**
  * {@inheritdoc}
  */
 public function get($productSku, $optionId)
 {
     $product = $this->productRepository->get($productSku);
     $option = $product->getOptionById($optionId);
     if ($option === null) {
         throw new NoSuchEntityException();
     }
     return $this->_createOptionDataObject($option);
 }
示例#15
0
 /**
  * {@inheritdoc}
  */
 public function get($sku, $editMode = false, $storeId = null, $forceReload = false)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'get');
     if (!$pluginInfo) {
         return parent::get($sku, $editMode, $storeId, $forceReload);
     } else {
         return $this->___callPlugins('get', func_get_args(), $pluginInfo);
     }
 }
 public function testGetBySkuFromCacheInitializedInGetById()
 {
     $productId = 123;
     $productSku = 'product_123';
     $this->productFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->productMock));
     $this->productMock->expects($this->once())->method('load')->with($productId);
     $this->productMock->expects($this->once())->method('getId')->willReturn($productId);
     $this->productMock->expects($this->once())->method('getSku')->willReturn($productSku);
     $this->assertEquals($this->productMock, $this->model->getById($productId));
     $this->assertEquals($this->productMock, $this->model->get($productSku));
 }
示例#17
0
 /**
  * {@inheritdoc}
  */
 public function getSamples($productSku)
 {
     $sampleList = [];
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->productRepository->get($productSku);
     $samples = $this->downloadableType->getSamples($product);
     /** @var \Magento\Downloadable\Model\Sample $sample */
     foreach ($samples as $sample) {
         $sampleList[] = $this->buildSample($sample);
     }
     return $sampleList;
 }
示例#18
0
 /**
  * {@inheritdoc}
  */
 public function removeChild($productSku, $childSku)
 {
     $product = $this->productRepository->get($productSku);
     if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
         throw new Exception(sprintf('Product with specified sku: %s is not a configurable product', $productSku), Exception::HTTP_FORBIDDEN);
     }
     $options = $product->getTypeInstance()->getUsedProducts($product);
     $ids = array();
     foreach ($options as $option) {
         if ($option->getSku() == $childSku) {
             continue;
         }
         $ids[] = $option->getId();
     }
     if (count($options) == count($ids)) {
         throw new NoSuchEntityException('Requested option doesn\'t exist');
     }
     $product->addData(['associated_product_ids' => $ids]);
     $product->save();
     return true;
 }
示例#19
0
 /**
  * {@inheritdoc}
  */
 public function getList($productSku)
 {
     $product = $this->productRepository->get($productSku, true);
     $priceKey = 'website_price';
     if ($this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE) == 0) {
         $priceKey = 'price';
     }
     $prices = array();
     foreach ($product->getData('group_price') as $price) {
         $this->groupPriceBuilder->populateWithArray(array(Product\GroupPrice::CUSTOMER_GROUP_ID => $price['all_groups'] ? 'all' : $price['cust_group'], Product\GroupPrice::VALUE => $price[$priceKey]));
         $prices[] = $this->groupPriceBuilder->create();
     }
     return $prices;
 }
示例#20
0
 /**
  * {@inheritdoc}
  */
 public function getList($productSku, $customerGroupId)
 {
     $product = $this->productRepository->get($productSku, true);
     $priceKey = 'website_price';
     if ($this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE) == 0) {
         $priceKey = 'price';
     }
     $prices = array();
     foreach ($product->getData('tier_price') as $price) {
         if (is_numeric($customerGroupId) && intval($price['cust_group']) === intval($customerGroupId) || $customerGroupId === 'all' && $price['all_groups']) {
             $this->priceBuilder->populateWithArray(array(Product\TierPrice::VALUE => $price[$priceKey], Product\TierPrice::QTY => $price['price_qty']));
             $prices[] = $this->priceBuilder->create();
         }
     }
     return $prices;
 }
 /**
  * Update bundle-related attributes of product.
  *
  * @param string $sku
  * @param Product $updatedProduct
  * @return string
  */
 public function update($sku, Product $updatedProduct)
 {
     /**
      * @var Product $existingProduct
      */
     $existingProduct = $this->productRepository->get($sku, true);
     if ($existingProduct->getTypeId() != ProductType::TYPE_BUNDLE) {
         return $sku;
     }
     /**
      * @var Option[] $existingProductOptions
      */
     $existingProductOptions = $this->optionReadService->getList($sku);
     /**
      * @var Option[] $newProductOptions
      */
     /**
      * @var AttributeValue $newProductOptionsAttrValue
      */
     $newProductOptionsAttrValue = $updatedProduct->getCustomAttribute('bundle_product_options');
     if (is_null($newProductOptionsAttrValue) || !is_array($newProductOptionsAttrValue->getValue())) {
         $newProductOptions = array();
     } else {
         $newProductOptions = $newProductOptionsAttrValue->getValue();
     }
     /**
      * @var Option[] $optionsToDelete
      */
     $optionsToDelete = array_udiff($existingProductOptions, $newProductOptions, array($this, 'compareOptions'));
     foreach ($optionsToDelete as $option) {
         $this->optionWriteService->remove($sku, $option->getId());
     }
     /** @var Option[] $optionsToUpdate */
     $optionsToUpdate = array_uintersect($existingProductOptions, $newProductOptions, array($this, 'compareOptions'));
     foreach ($optionsToUpdate as $option) {
         $this->optionWriteService->update($sku, $option->getId(), $option);
     }
     /**
      * @var Option[] $optionsToAdd
      */
     $optionsToAdd = array_udiff($newProductOptions, $existingProductOptions, array($this, 'compareOptions'));
     foreach ($optionsToAdd as $option) {
         $this->optionWriteService->add($sku, $option);
     }
     return $sku;
 }
 /**
  * @depends testAfterSave
  */
 public function testAfterSaveEmpty()
 {
     \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\StoreManagerInterface')->setCurrentStore(\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\StoreManagerInterface')->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID));
     /** @var $product \Magento\Catalog\Model\Product */
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $product->load($this->productRepository->get('simple')->getId());
     $product->setOrigData();
     $product->setTierPrice([]);
     $this->_model->afterSave($product);
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $fixtureProduct = $this->productRepository->get('simple');
     $product->setId($fixtureProduct->getId());
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $product->setData($linkField, $fixtureProduct->getData($linkField));
     $this->_model->afterLoad($product);
     $this->assertEmpty($product->getTierPrice());
 }
示例#23
0
 /**
  * {@inheritdoc}
  */
 public function update($productSku, $optionId, \Magento\Catalog\Service\V1\Product\CustomOptions\Data\Option $option)
 {
     $product = $this->productRepository->get($productSku);
     $optionData = $this->optionConverter->convert($option);
     if (!$product->getOptionById($optionId)) {
         throw new NoSuchEntityException();
     }
     $optionData['option_id'] = $optionId;
     $originalValues = $product->getOptionById($optionId)->getValues();
     if (array_key_exists('values', $optionData)) {
         $optionData['values'] = $this->markRemovedValues($optionData['values'], $originalValues);
     }
     $product->setCanSaveCustomOptions(true);
     $product->setProductOptions([$optionData]);
     try {
         $product->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not save custom option');
     }
     return true;
 }
示例#24
0
 /**
  * {@inheritdoc}
  */
 public function info($productSku, $imageId)
 {
     try {
         $product = $this->productRepository->get($productSku);
     } catch (\Exception $exception) {
         throw new NoSuchEntityException("Such product doesn't exist");
     }
     $output = null;
     $productImages = $this->getMediaAttributeValues($product);
     foreach ((array) $product->getMediaGallery('images') as $image) {
         if (intval($image['value_id']) == intval($imageId)) {
             $image['types'] = array_keys($productImages, $image['file']);
             $output = $this->galleryEntryBuilder->populateWithArray($image)->create();
             break;
         }
     }
     if (is_null($output)) {
         throw new NoSuchEntityException("Such image doesn't exist");
     }
     return $output;
 }
示例#25
0
 protected function _requireCustomerWithTwoProducts()
 {
     $customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Model\\Customer');
     /** @var \Magento\Customer\Model\Customer $customer */
     $customer->setWebsiteId(1)->setId(1)->setEntityTypeId(1)->setAttributeSetId(1)->setEmail('*****@*****.**')->setPassword('password')->setGroupId(1)->setStoreId(1)->setIsActive(1)->setFirstname('Firstname')->setLastname('Lastname')->setDefaultBilling(1)->setDefaultShipping(1);
     $customer->isObjectNew(true);
     $customer->save();
     /** @var $session \Magento\Customer\Model\Session */
     $session = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Customer\\Model\\Session');
     $session->setCustomerId(1);
     /** @var $visitor \Magento\Customer\Model\Visitor */
     $visitor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Model\\Visitor');
     $visitor->setSessionId(md5(time()) . md5(microtime()))->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))->save();
     $firstProductEntityId = $this->productRepository->get('simple_product_1')->getEntityId();
     $secondProductEntityId = $this->productRepository->get('simple_product_2')->getEntityId();
     /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
     $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product\\Compare\\Item');
     $item->setVisitorId($visitor->getId())->setCustomerId(1)->setProductId($firstProductEntityId)->save();
     /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
     $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product\\Compare\\Item');
     $item->setVisitorId($visitor->getId())->setProductId($secondProductEntityId)->save();
     \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Customer\\Model\\Visitor')->load($visitor->getId());
     $this->_assertCompareListEquals([$firstProductEntityId, $secondProductEntityId]);
 }