public function testGetJsonConfig() { $config = (array) json_decode($this->_block->getJsonConfig()); $this->assertNotEmpty($config); $this->assertArrayHasKey('productId', $config); $this->assertEquals($this->_product->getId(), $config['productId']); }
/** * @magentoDbIsolation enabled * @magentoAppIsolation enabled * @magentoDataFixture Magento/CatalogRule/_files/attribute.php * @magentoDataFixture Magento/CatalogRule/_files/rule_by_attribute.php * @magentoDataFixture Magento/Catalog/_files/product_simple.php */ public function testReindexFull() { $this->prepareProducts(); $this->indexerBuilder->reindexFull(); $this->assertEquals(9.800000000000001, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->product->getId())); $this->assertEquals(9.800000000000001, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productSecond->getId())); $this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productThird->getId())); }
public function testGetAddToWishlistParams() { $json = $this->block->getAddToWishlistParams($this->product); $params = (array) json_decode($json); $data = (array) $params['data']; $this->assertEquals($this->product->getId(), $data['product']); $this->assertArrayHasKey('uenc', $data); $this->assertStringEndsWith('wishlist/index/add/', $params['action']); }
/** * The only rewritten line in this method is the return statement */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); $categoryId = null; if (!isset($params['_ignore_category']) && $product->getCategoryId() && !$product->getDoNotUseCategoryId()) { $categoryId = $product->getCategoryId(); } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $filterData = [UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId]; if ($categoryId) { $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = []; } /* * This is the only line changed from the default method. * For reference, the original line: $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); * getUrlInstance() is a private method, so a new method has been written that will create a frontend Url object if * the store scope is not the admin scope. */ return $this->getStoreScopeUrlInstance($storeId)->getUrl($routePath, $routeParams); }
public function beforeProcess() { $product = $this->getEvent()->getProduct(); if (!$product instanceof \Magento\Catalog\Model\Product) { throw new \Ess\M2ePro\Model\Exception('Product event doesn\'t have correct Product instance.'); } $this->product = $product; $this->productId = (int) $this->product->getId(); $this->storeId = (int) $this->product->getData('store_id'); }
/** * Return Product attribute by attribute's ID * * @param \Magento\Catalog\Model\Product $product * @param int $attributeId * @return null|\Magento\Catalog\Model\Entity\Attribute Product's attribute */ public function getProductAttribute(\Magento\Catalog\Model\Product $product, $attributeId) { if (!isset($this->_productAttributes[$product->getId()])) { $attributes = $product->getAttributes(); foreach ($attributes as $attribute) { $this->_productAttributes[$product->getId()][$attribute->getAttributeId()] = $attribute; } } return isset($this->_productAttributes[$product->getId()][$attributeId]) ? $this->_productAttributes[$product->getId()][$attributeId] : null; }
public function testGetProduct() { $this->assertNotEmpty($this->_block->getProduct()->getId()); $this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId()); /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $objectManager->get('Magento\\Framework\\Registry')->unregister('product'); $this->_block->setProductId(1); $this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId()); }
/** * @param array $productsData * @dataProvider generateSimpleProductsWithPartialDataDataProvider * @magentoDbIsolation enabled */ public function testGenerateSimpleProductsWithPartialData($productsData) { $this->_product->setNewVariationsAttributeSetId(4); $generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData); $parentStockItem = $this->stockRegistry->getStockItem($this->_product->getId()); foreach ($generatedProducts as $productId) { $stockItem = $this->stockRegistry->getStockItem($productId); $this->assertEquals($parentStockItem->getManageStock(), $stockItem->getManageStock()); $this->assertEquals('1', $stockItem->getIsInStock()); } }
/** * @depends testReindexRowAfterMassAction * @magentoAppArea adminhtml */ public function testReindexRowAfterDelete() { $this->productSecond->delete(); $products = $this->search('Simple Product Common'); $this->assertCount(1, $products); $this->assertEquals($this->productFirst->getId(), $products[0]->getId()); }
/** * @param Product $product * @param int $attributeId * @return array */ public function loadProductGalleryByAttributeId($product, $attributeId) { $select = $this->createBaseLoadSelect($product->getId(), $product->getStoreId(), $attributeId); $result = $this->getConnection()->fetchAll($select); $this->_removeDuplicates($result); return $result; }
/** * Create product duplicate * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product */ public function copy(\Magento\Catalog\Model\Product $product) { $product->getWebsiteIds(); $product->getCategoryIds(); $duplicate = $this->productFactory->create(); $duplicate->setData($product->getData()); $duplicate->setIsDuplicate(true); $duplicate->setOriginalId($product->getId()); $duplicate->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED); $duplicate->setCreatedAt(null); $duplicate->setUpdatedAt(null); $duplicate->setId(null); $duplicate->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); $this->copyConstructor->build($product, $duplicate); $isDuplicateSaved = false; do { $urlKey = $duplicate->getUrlKey(); $urlKey = preg_match('/(.*)-(\\d+)$/', $urlKey, $matches) ? $matches[1] . '-' . ($matches[2] + 1) : $urlKey . '-1'; $duplicate->setUrlKey($urlKey); try { $duplicate->save(); $isDuplicateSaved = true; } catch (\Magento\Framework\Exception\AlreadyExistsException $e) { } } while (!$isDuplicateSaved); $product->getOptionInstance()->duplicate($product->getId(), $duplicate->getId()); $product->getResource()->duplicate($product->getId(), $duplicate->getId()); return $duplicate; }
/** * @param CatalogProduct $product * @param array $identities * @return string[] */ public function afterGetIdentities(CatalogProduct $product, array $identities) { foreach ($this->type->getParentIdsByChild($product->getId()) as $parentId) { $identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId; } return $identities; }
/** * @param \Magento\Catalog\Model\Product $productModel * @return \Ess\M2ePro\Model\Magento\Product */ public function setProduct(\Magento\Catalog\Model\Product $productModel) { $this->_productModel = $productModel; $this->setProductId($this->_productModel->getId()); $this->setStoreId($this->_productModel->getStoreId()); return $this; }
public function deleteProduct(\Magento\Catalog\Model\Product $product, $deletingMode) { if ($deletingMode == \Ess\M2ePro\Model\Listing::DELETING_MODE_NONE) { return; } $listingsProducts = $this->getListing()->getProducts(true, array('product_id' => (int) $product->getId())); if (count($listingsProducts) <= 0) { return; } foreach ($listingsProducts as $listingProduct) { if (!$listingProduct instanceof \Ess\M2ePro\Model\Listing\Product) { return; } try { if ($deletingMode == \Ess\M2ePro\Model\Listing::DELETING_MODE_STOP) { $listingProduct->isStoppable() && $this->activeRecordFactory->getObject('StopQueue')->add($listingProduct); } if ($deletingMode == \Ess\M2ePro\Model\Listing::DELETING_MODE_STOP_REMOVE) { $listingProduct->isStoppable() && $this->activeRecordFactory->getObject('StopQueue')->add($listingProduct); $listingProduct->addData(array('status' => \Ess\M2ePro\Model\Listing\Product::STATUS_STOPPED))->save(); $listingProduct->delete(); } } catch (\Exception $exception) { } } }
/** * Apply catalog rules after product save * * @param Product $subject * @param Product $result * @return Product * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterSave(Product $subject, Product $result) { if (!$result->getIsMassupdate()) { $this->productRuleProcessor->reindexRow($result->getId()); } return $result; }
/** * Generate list based on categories * * @param int $storeId * @param Product $product * @param ObjectRegistry $productCategories * @return UrlRewrite[] */ public function generate($storeId, Product $product, ObjectRegistry $productCategories) { $urls = []; foreach ($productCategories->getList() as $category) { $urls[] = $this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($product->getId())->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category))->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))->setStoreId($storeId)->setMetadata(['category_id' => $category->getId()]); } return $urls; }
/** * @param UrlRewrite $url * @param int $storeId * @param Category|null $category * @return array */ protected function generateForCustom($url, $storeId, $category) { $targetPath = $url->getRedirectType() ? $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() === $targetPath) { return []; } return [$this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->product->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType($url->getRedirectType())->setStoreId($storeId)->setDescription($url->getDescription())->setIsAutogenerated(0)->setMetadata($url->getMetadata())]; }
/** * @magentoDbIsolation disabled * @magentoAppIsolation enabled * @magentoDataFixture Magento/Catalog/_files/price_row_fixture.php */ public function testProductsUpdate() { $this->_product->load(1); $this->_processor->reindexList([$this->_product->getId()]); $categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\CategoryFactory'); $listProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Block\\Product\\ListProduct'); $category = $categoryFactory->create()->load(9); $layer = $listProduct->getLayer(); $layer->setCurrentCategory($category); $productCollection = $layer->getProductCollection(); $this->assertEquals(1, $productCollection->count()); /** @var $product \Magento\Catalog\Model\Product */ foreach ($productCollection as $product) { $this->assertEquals($this->_product->getId(), $product->getId()); $this->assertEquals($this->_product->getPrice(), $product->getPrice()); } }
/** * @param \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product * @param array $modelData * @return \Magento\Downloadable\Model\Sample|\PHPUnit_Framework_MockObject_MockObject */ private function createSampleModel($product, array $modelData) { $sample = $this->getMockBuilder('\\Magento\\Downloadable\\Model\\Sample')->disableOriginalConstructor()->setMethods(['setData', 'setSampleType', 'setProductId', 'setStoreId', 'setProductWebsiteIds', 'setNumberOfDownloads', 'setSampleUrl', 'setLinkFile', 'setSampleFile', 'save'])->getMock(); $sample->expects($this->once())->method('setData')->with($modelData)->will($this->returnSelf()); $sample->expects($this->once())->method('setSampleType')->with($modelData['type'])->will($this->returnSelf()); $sample->expects($this->once())->method('setProductId')->with($product->getId())->will($this->returnSelf()); $sample->expects($this->once())->method('setStoreId')->with($product->getStoreId())->will($this->returnSelf()); return $sample; }
/** * @magentoDbIsolation enabled * @magentoAppIsolation enabled * @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1 * @magentoDataFixture Magento/Catalog/_files/product_simple.php */ public function testProductsUpdate() { $this->markTestSkipped('Should be fixed in MAGETWO-25641'); $this->_product->load(1); $this->_processor->reindexList(array($this->_product->getId())); $categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\CategoryFactory'); $listProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Block\\Product\\ListProduct'); $category = $categoryFactory->create()->load(2); $layer = $listProduct->getLayer(); $layer->setCurrentCategory($category); $productCollection = $layer->getProductCollection(); $this->assertCount(1, $productCollection); /** @var $product \Magento\Catalog\Model\Product */ foreach ($productCollection as $product) { $this->assertEquals($this->_product->getName(), $product->getName()); $this->assertEquals($this->_product->getShortDescription(), $product->getShortDescription()); } }
/** * Copy product inventory data (used for product duplicate functionality) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product $duplicate * @return void */ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate) { $stockData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1]; $currentStockItemDo = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); if ($currentStockItemDo->getItemId()) { $stockData += ['use_config_enable_qty_inc' => $currentStockItemDo->getUseConfigEnableQtyInc(), 'enable_qty_increments' => $currentStockItemDo->getEnableQtyIncrements(), 'use_config_qty_increments' => $currentStockItemDo->getUseConfigQtyIncrements(), 'qty_increments' => $currentStockItemDo->getQtyIncrements()]; } $duplicate->setStockData($stockData); }
/** * Assign stock status information to product * * @param \Magento\Catalog\Model\Product $product * @param int $stockStatus * @return void */ public function assignStatusToProduct(\Magento\Catalog\Model\Product $product, $stockStatus = null) { if ($stockStatus === null) { $websiteId = $product->getStore()->getWebsiteId(); $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } $product->setIsSalable($status); }
/** * Assign stock status information to product * * @param Product $product * @param int $stockStatus * @return void */ public function assignStatusToProduct(Product $product, $stockStatus = null) { if ($stockStatus === null) { $websiteId = $product->getStore()->getWebsiteId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } $product->setIsSalable($status); }
/** * Save Product Links process * * @param \Magento\Catalog\Model\Product $product * @param array $data * @param int $typeId * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function saveProductLinks($product, $data, $typeId) { if (!is_array($data)) { $data = []; } $attributes = $this->getAttributesByType($typeId); $connection = $this->getConnection(); $bind = [':product_id' => (int) $product->getId(), ':link_type_id' => (int) $typeId]; $select = $connection->select()->from($this->getMainTable(), ['linked_product_id', 'link_id'])->where('product_id = :product_id')->where('link_type_id = :link_type_id'); $links = $connection->fetchPairs($select, $bind); $deleteIds = []; foreach ($links as $linkedProductId => $linkId) { if (!isset($data[$linkedProductId])) { $deleteIds[] = (int) $linkId; } } if (!empty($deleteIds)) { $connection->delete($this->getMainTable(), ['link_id IN (?)' => $deleteIds]); } foreach ($data as $linkedProductId => $linkInfo) { $linkId = null; if (isset($links[$linkedProductId])) { $linkId = $links[$linkedProductId]; unset($links[$linkedProductId]); } else { $bind = ['product_id' => $product->getId(), 'linked_product_id' => $linkedProductId, 'link_type_id' => $typeId]; $connection->insert($this->getMainTable(), $bind); $linkId = $connection->lastInsertId($this->getMainTable()); } foreach ($attributes as $attributeInfo) { $attributeTable = $this->getAttributeTypeTable($attributeInfo['type']); if ($attributeTable) { if (isset($linkInfo[$attributeInfo['code']])) { $value = $this->_prepareAttributeValue($attributeInfo['type'], $linkInfo[$attributeInfo['code']]); $bind = ['product_link_attribute_id' => $attributeInfo['id'], 'link_id' => $linkId, 'value' => $value]; $connection->insertOnDuplicate($attributeTable, $bind, ['value']); } else { $connection->delete($attributeTable, ['link_id = ?' => $linkId, 'product_link_attribute_id = ?' => $attributeInfo['id']]); } } } } return $this; }
/** * Copy product inventory data (used for product duplicate functionality) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product $duplicate * @return void */ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate) { $stockData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1]; /** @var \Magento\CatalogInventory\Service\V1\Data\StockItem $currentStockItemDo */ $currentStockItemDo = $this->stockItemService->getStockItem($product->getId()); if ($currentStockItemDo->getStockId()) { $stockData += ['use_config_enable_qty_inc' => $currentStockItemDo->isUseConfigEnableQtyInc(), 'enable_qty_increments' => $currentStockItemDo->isEnableQtyIncrements(), 'use_config_qty_increments' => $currentStockItemDo->isUseConfigQtyIncrements(), 'qty_increments' => $currentStockItemDo->getQtyIncrements()]; } $duplicate->setStockData($stockData); }
/** * @param \Magento\Catalog\Model\Product $product */ private function assertProductInfo($product) { $data = [1 => ['sku' => 'simple', 'name' => 'Simple Product', 'price' => '10', 'qty' => '1', 'position' => '1'], 21 => ['sku' => 'virtual-product', 'name' => 'Virtual Product', 'price' => '10', 'qty' => '2', 'position' => '2']]; $productId = $product->getId(); $this->assertEquals($data[$productId]['sku'], $product->getSku()); $this->assertEquals($data[$productId]['name'], $product->getName()); $this->assertEquals($data[$productId]['price'], $product->getPrice()); $this->assertEquals($data[$productId]['qty'], $product->getQty()); $this->assertEquals($data[$productId]['position'], $product->getPosition()); }
/** * Load gallery images for product * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product\Attribute\Backend\Media $object * @return array */ public function loadGallery($product, $object) { $adapter = $this->_getReadAdapter(); $positionCheckSql = $adapter->getCheckSql('value.position IS NULL', 'default_value.position', 'value.position'); // Select gallery images for product $select = $adapter->select()->from(array('main' => $this->getMainTable()), array('value_id', 'value AS file'))->joinLeft(array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)), $adapter->quoteInto('main.value_id = value.value_id AND value.store_id = ?', (int) $product->getStoreId()), array('label', 'position', 'disabled'))->joinLeft(array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)), 'main.value_id = default_value.value_id AND default_value.store_id = 0', array('label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled'))->where('main.attribute_id = ?', $object->getAttribute()->getId())->where('main.entity_id = ?', $product->getId())->order($positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC); $result = $adapter->fetchAll($select); $this->_removeDuplicates($result); return $result; }
/** * Generate list of urls for global scope * * @param \Magento\Framework\Data\Collection $productCategories * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ protected function generateForGlobalScope($productCategories) { $urls = []; $productId = $this->product->getId(); foreach ($this->product->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY)) { $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories)); } } return $urls; }
/** * @magentoAppArea adminhtml */ public function testReindexRowAfterDelete() { $this->testReindexRowAfterEdit(); $this->productBanana->delete(); $products = $this->search('Simple Product'); $this->assertCount(4, $products); $this->assertEquals($this->productApple->getId(), $products[0]->getId()); $this->assertEquals($this->productOrange->getId(), $products[1]->getId()); $this->assertEquals($this->productPapaya->getId(), $products[2]->getId()); $this->assertEquals($this->productCherry->getId(), $products[3]->getId()); }
/** * @param \Magento\Catalog\Model\Product $product * @return int[] */ protected function getLinkedProducts(\Magento\Catalog\Model\Product $product) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $typeInstance */ $typeInstance = $product->getTypeInstance(); $childrenIds = $typeInstance->getChildrenIds($product->getId()); if (isset($childrenIds[0])) { return $childrenIds[0]; } else { return []; } }