/** * Generate product url rewrites * * @param \Magento\Catalog\Model\Product $product * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ public function generate(Product $product) { $this->product = $product; $storeId = $this->product->getStoreId(); $productCategories = $product->getCategoryCollection()->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); $urls = $this->isGlobalScope($storeId) ? $this->generateForGlobalScope($productCategories) : $this->generateForSpecificStoreView($storeId, $productCategories); $this->product = null; return $urls; }
/** * Get categories assigned to product * * @return \Magento\Catalog\Model\Resource\Category\Collection */ protected function getCategories() { if (!$this->categories) { $this->categories = $this->product->getCategoryCollection(); $this->categories->addAttributeToSelect('url_key'); $this->categories->addAttributeToSelect('url_path'); } return $this->categories; }
public function testGetCategoryCollection() { // empty $collection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection', $collection); // fixture $this->_model->setId($this->productRepository->get('simple')->getId()); $fixtureCollection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection', $fixtureCollection); $this->assertNotSame($fixtureCollection, $collection); $ids = []; foreach ($fixtureCollection as $category) { $ids[] = $category->getId(); } $this->assertEquals([2, 3, 4, 13], $ids); }
public function testGetCategoryCollection() { // empty $collection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Magento\\Catalog\\Model\\Resource\\Category\\Collection', $collection); // fixture $this->_model->setId(1); $fixtureCollection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Magento\\Catalog\\Model\\Resource\\Category\\Collection', $fixtureCollection); $this->assertNotSame($fixtureCollection, $collection); $ids = array(); foreach ($fixtureCollection as $category) { $ids[] = $category->getId(); } $this->assertEquals(array(2, 3, 4), $ids); }
public function testSetCategoryCollection() { $collection = $this->getMockBuilder('\\Magento\\Framework\\Data\\Collection')->disableOriginalConstructor()->getMock(); $this->resource->expects($this->once())->method('getCategoryCollection')->will($this->returnValue($collection)); $this->assertSame($this->model->getCategoryCollection(), $this->model->getCategoryCollection()); }
/** * Generate product url rewrites * * @param \Magento\Catalog\Model\Product $product * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ public function generate(Product $product) { if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) { return []; } $this->product = $product; $storeId = $this->product->getStoreId(); $productCategories = $product->getCategoryCollection()->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); $urls = $this->isGlobalScope($storeId) ? $this->generateForGlobalScope($productCategories) : $this->generateForSpecificStoreView($storeId, $productCategories); $this->product = null; return $urls; }
/** * {@inheritdoc} */ public function getCategoryCollection() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getCategoryCollection'); if (!$pluginInfo) { return parent::getCategoryCollection(); } else { return $this->___callPlugins('getCategoryCollection', func_get_args(), $pluginInfo); } }
/** * @param Product $product * @return array */ protected function buildCategories(Product $product) { $categories = []; foreach ($product->getCategoryCollection() as $category) { $categories[] = $this->_categoryBuilder->build($category); } return $categories; }