/** * @return array */ public function getCartItems() { if (!isset(self::$quoteItemsData)) { self::$quoteItemsData = []; $displayPriceWithTax = $this->rejoinerHelper->getTrackPriceWithTax(); if ($quote = $this->getQuote()) { $categories = []; /** @var \Magento\Quote\Model\Quote $quote */ /** @var \Magento\Quote\Model\Quote\Item $item */ foreach ($quote->getAllItems() as $item) { $categories = array_merge($categories, $item->getProduct()->getCategoryIds()); } /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection */ $categoryCollection = $this->categoryCollectionFactory->create(); $categoriesArray = $categoryCollection->addAttributeToSelect('name')->addFieldToFilter('entity_id', ['in' => $categories])->load()->getItems(); $imageWidth = $this->rejoinerHelper->getImageWidth(); $imageHeight = $this->rejoinerHelper->getImageHeight(); foreach ($quote->getAllVisibleItems() as $item) { $product = $item->getProduct(); $productCategories = $this->rejoinerHelper->getProductCategories($product, $categoriesArray); $imageUrl = $this->imageHelper->init($product, 'category_page_grid')->resize($imageWidth, $imageHeight)->getUrl(); if ($displayPriceWithTax) { $productPrice = $item->getPriceInclTax(); $rowTotal = $item->getRowTotalInclTax(); } else { $productPrice = $item->getPrice(); $rowTotal = $item->getRowTotal(); } $newItem = ['name' => $item->getName(), 'image_url' => $imageUrl, 'price' => (string) $this->rejoinerHelper->convertPriceToCents($productPrice), 'product_id' => (string) $item->getSku(), 'item_qty' => (string) $item->getQty(), 'qty_price' => (string) $this->rejoinerHelper->convertPriceToCents($rowTotal), 'product_url' => (string) $product->getProductUrl(), 'category' => $productCategories]; self::$quoteItemsData[] = $newItem; } } } return self::$quoteItemsData; }
/** * Retrieve categories tree * * @return array */ protected function getCategoriesTree() { if ($this->categoriesTree === null) { $storeId = $this->request->getParam('store'); /* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $matchingNamesCollection = $this->categoryCollectionFactory->create(); $matchingNamesCollection->addAttributeToSelect('path')->addAttributeToFilter('entity_id', ['neq' => CategoryModel::TREE_ROOT_ID])->setStoreId($storeId); $shownCategoriesIds = []; /** @var \Magento\Catalog\Model\Category $category */ foreach ($matchingNamesCollection as $category) { foreach (explode('/', $category->getPath()) as $parentId) { $shownCategoriesIds[$parentId] = 1; } } /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', ['in' => array_keys($shownCategoriesIds)])->addAttributeToSelect(['name', 'is_active', 'parent_id'])->setStoreId($storeId); $categoryById = [CategoryModel::TREE_ROOT_ID => ['value' => CategoryModel::TREE_ROOT_ID]]; foreach ($collection as $category) { foreach ([$category->getId(), $category->getParentId()] as $categoryId) { if (!isset($categoryById[$categoryId])) { $categoryById[$categoryId] = ['value' => $categoryId]; } } $categoryById[$category->getId()]['is_active'] = $category->getIsActive(); $categoryById[$category->getId()]['label'] = $category->getName(); $categoryById[$category->getParentId()]['optgroup'][] =& $categoryById[$category->getId()]; } $this->categoriesTree = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']; } return $this->categoriesTree; }
/** * @param \Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory * @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory * @param \Magento\Eav\Model\Config $eavConfig * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory */ public function __construct(\Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory) { $this->categoryTree = $categoryTreeFactory->create(['categoryTree' => $categoryResourceTreeFactory->create(), 'categoryCollection' => $categoryCollectionFactory->create()]); $this->eavConfig = $eavConfig; $this->attributeCollectionFactory = $attributeCollectionFactory; $this->attrOptionCollectionFactory = $attrOptionCollectionFactory; $this->productCollection = $productCollectionFactory->create(); }
public function testToOptionArray() { $matchingNamesCollection = $this->getCategoryCollectionMock([$this->getCategoryMock(['path' => '1/2']), $this->getCategoryMock(['path' => '1/3']), $this->getCategoryMock(['path' => '1/3/4']), $this->getCategoryMock(['path' => '1/2/5']), $this->getCategoryMock(['path' => '1/3/4/6']), $this->getCategoryMock(['path' => '1/7']), $this->getCategoryMock(['path' => '1/8']), $this->getCategoryMock(['path' => '1/7/9'])]); $collection = $this->getCategoryCollectionMock([$this->getCategoryMock(['id' => '2', 'parent_id' => '1', 'name' => 'Category 2', 'is_active' => '1']), $this->getCategoryMock(['id' => '3', 'parent_id' => '1', 'name' => 'Category 3', 'is_active' => '0']), $this->getCategoryMock(['id' => '4', 'parent_id' => '3', 'name' => 'Category 4', 'is_active' => '1']), $this->getCategoryMock(['id' => '5', 'parent_id' => '2', 'name' => 'Category 5', 'is_active' => '1']), $this->getCategoryMock(['id' => '6', 'parent_id' => '4', 'name' => 'Category 6', 'is_active' => '1']), $this->getCategoryMock(['id' => '7', 'parent_id' => '1', 'name' => 'Category 7', 'is_active' => '0']), $this->getCategoryMock(['id' => '8', 'parent_id' => '1', 'name' => 'Category 8', 'is_active' => '1']), $this->getCategoryMock(['id' => '9', 'parent_id' => '7', 'name' => 'Category 9', 'is_active' => '1'])]); $result = [['value' => '2', 'is_active' => '1', 'label' => 'Category 2', 'optgroup' => [['value' => '5', 'is_active' => '1', 'label' => 'Category 5']]], ['value' => '3', 'is_active' => '0', 'label' => 'Category 3', 'optgroup' => [['value' => '4', 'is_active' => '1', 'label' => 'Category 4', 'optgroup' => [['value' => '6', 'is_active' => '1', 'label' => 'Category 6']]]]], ['value' => '7', 'is_active' => '0', 'label' => 'Category 7', 'optgroup' => [['value' => '9', 'is_active' => '1', 'label' => 'Category 9']]], ['value' => '8', 'is_active' => '1', 'label' => 'Category 8']]; $this->categoryCollectionFactoryMock->expects($this->any())->method('create')->willReturnOnConsecutiveCalls($matchingNamesCollection, $collection); $this->assertSame($result, $this->categoriesOptions->toOptionArray()); }
/** * get categories array. * * @return array */ public function getCategoriesArray() { $categoriesArray = $this->_categoryCollectionFactory->create()->addAttributeToSelect('name')->addAttributeToSort('path', 'asc')->load()->toArray(); $categories = array(); foreach ($categoriesArray as $categoryId => $category) { if (isset($category['name']) && isset($category['level'])) { $categories[] = array('label' => $category['name'], 'level' => $category['level'], 'value' => $categoryId); } } return $categories; }
/** * Return option array * * @param bool $addEmpty * @return array */ public function toOptionArray($addEmpty = true) { /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ $collection = $this->_categoryCollectionFactory->create(); $collection->addAttributeToSelect('name')->addRootLevelFilter()->load(); $options = []; if ($addEmpty) { $options[] = ['label' => __('-- Please Select a Category --'), 'value' => '']; } foreach ($collection as $category) { $options[] = ['label' => $category->getName(), 'value' => $category->getId()]; } return $options; }
protected function setUp() { parent::setUp(); $this->categoryCollectionFactoryMock = $this->getMockBuilder(CategoryCollectionFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->dbHelperMock = $this->getMockBuilder(DbHelper::class)->disableOriginalConstructor()->getMock(); $this->urlBuilderMock = $this->getMockBuilder(UrlInterface::class)->getMockForAbstractClass(); $this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); $this->categoryCollectionMock = $this->getMockBuilder(CategoryCollection::class)->disableOriginalConstructor()->getMock(); $this->categoryCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->categoryCollectionMock); $this->categoryCollectionMock->expects($this->any())->method('addAttributeToSelect')->willReturnSelf(); $this->categoryCollectionMock->expects($this->any())->method('addAttributeToFilter')->willReturnSelf(); $this->categoryCollectionMock->expects($this->any())->method('setStoreId')->willReturnSelf(); $this->categoryCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([])); }
/** * Return parent categories of category * * @param \Magento\Catalog\Model\Category $category * @return \Magento\Framework\DataObject[] */ public function getParentCategories($category) { $pathIds = array_reverse(explode(',', $category->getPathInStore())); /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categories */ $categories = $this->_categoryCollectionFactory->create(); return $categories->setStore($this->_storeManager->getStore())->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addFieldToFilter('entity_id', ['in' => $pathIds])->addFieldToFilter('is_active', 1)->load()->getItems(); }
/** * Get collection of product categories * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\ResourceModel\Category\Collection */ public function getCategoryCollection($product) { /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ $collection = $this->_categoryCollectionFactory->create(); $collection->joinField('product_id', 'catalog_category_product', 'product_id', 'category_id = entity_id', null)->addFieldToFilter('product_id', (int) $product->getId()); return $collection; }
/** * {@inheritdoc} */ public function getCount() { $categories = $this->categoriesFactory->create(); /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categories */ $categories->addAttributeToFilter('parent_id', ['gt' => 0]); return $categories->getSize(); }
public function testGetCount() { $categoriesMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection', [], [], '', false); $this->categoriesFactoryMock->expects($this->once())->method('create')->willReturn($categoriesMock); $categoriesMock->expects($this->once())->method('addAttributeToFilter')->with('parent_id', ['gt' => 0])->willReturnSelf(); $categoriesMock->expects($this->once())->method('getSize')->willReturn('expected'); $this->assertEquals('expected', $this->model->getCount()); }
/** * @param string $urlKey * @return mixed|null */ protected function getCategoryReplacement($urlKey) { $categoryCollection = $this->categoryCollectionFactory->create(); $category = $categoryCollection->addAttributeToFilter('url_key', $urlKey)->getFirstItem(); $categoryId = null; if (!empty($category)) { $categoryId = $category->getId(); } return $categoryId; }
/** * @return $this */ protected function initCategories() { if (empty($this->categories)) { $collection = $this->categoryColFactory->create(); $collection->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */ foreach ($collection as $category) { $structure = explode(self::DELIMITER_CATEGORY, $category->getPath()); $pathSize = count($structure); $this->categoriesCache[$category->getId()] = $category; if ($pathSize > 1) { $path = []; for ($i = 1; $i < $pathSize; $i++) { $path[] = $collection->getItemById((int) $structure[$i])->getName(); } $index = implode(self::DELIMITER_CATEGORY, $path); $this->categories[$index] = $category->getId(); } } } return $this; }
/** * Return array or collection of categories * * @param integer $parent * @param integer $recursionLevel * @param boolean|string $sorted * @param boolean $asCollection * @param boolean $toLoad * @return array|\Magento\Framework\Data\Collection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { if ($asCollection) { $select = $this->getConnection()->select()->from(['mt' => $this->getMainStoreTable($this->getStoreId())], ['path'])->where('mt.entity_id = ?', $parent); $parentPath = $this->getConnection()->fetchOne($select); $collection = $this->_categoryCollectionFactory->create()->addNameToResult()->addUrlRewriteToResult()->addParentPathFilter($parentPath)->addStoreFilter()->addIsActiveFilter()->addAttributeToFilter('include_in_menu', 1)->addSortedField($sorted); if ($toLoad) { return $collection->load(); } return $collection; } return $this->getNodes($parent, $recursionLevel, $this->_storeManager->getStore()->getId()); }
/** * {@inheritdoc} */ public function getSearchableEntities($storeId, $entityIds = null, $lastEntityId = null, $limit = 100) { /** @var \Magento\Store\Model\Store $store */ $store = $this->storeManager->getStore($storeId); $root = $store->getRootCategoryId(); $collection = $this->collectionFactory->create()->addAttributeToSelect(array_keys($this->getAttributes()))->setStoreId($storeId)->addPathsFilter("1/{$root}/")->addFieldToFilter('is_active', 1); if ($entityIds) { $collection->addFieldToFilter('entity_id', ['in' => $entityIds]); } $collection->addFieldToFilter('entity_id', ['gt' => $lastEntityId])->setPageSize($limit)->setOrder('entity_id'); foreach ($collection as $item) { $item->setData('description', $this->prepareHtml($item->getData('description'), $storeId)); $item->setData('landing_page', $this->renderCmsBlock($item->getData('landing_page'), $storeId)); } return $collection; }
/** * Get Category Tree * * @param int $storeId * @param int $rootId * @return \Magento\Catalog\Model\ResourceModel\Category\Collection * @throws \Magento\Framework\Exception\LocalizedException */ protected function getCategoryTree($storeId, $rootId) { /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ $collection = $this->collectionFactory->create(); $collection->setStoreId($storeId); $collection->addAttributeToSelect('name'); $collection->addFieldToFilter('path', ['like' => '1/' . $rootId . '/%']); //load only from store root $collection->addAttributeToFilter('include_in_menu', 1); $collection->addIsActiveFilter(); $collection->addUrlRewriteToResult(); $collection->addOrder('level', Collection::SORT_ORDER_ASC); $collection->addOrder('position', Collection::SORT_ORDER_ASC); $collection->addOrder('parent_id', Collection::SORT_ORDER_ASC); $collection->addOrder('entity_id', Collection::SORT_ORDER_ASC); return $collection; }
/** * @param string $attributes * @return \Magento\Catalog\Model\ResourceModel\Category\Collection */ public function getSelectedCategoriesCollection($attributes = '*') { if (is_null($this->categoryCollection)) { $collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToSelect($attributes); $collection->joinField('position', 'gemtoo_blog_article_category', 'position', 'category_id=entity_id', '{{table}}.article_id=' . $this->getId(), 'inner'); $this->categoryCollection = $collection; } return $this->categoryCollection; }
/** * @param string $urlKey * @return \Magento\Framework\DataObject */ protected function getCategoryByUrlKey($urlKey) { $category = $this->categoryFactory->create()->addAttributeToFilter('url_key', $urlKey)->addUrlRewriteToResult()->getFirstItem(); return $category; }
/** * Get categories collection * @return \Magento\Catalog\Model\ResourceModel\Category\Collection */ protected function _getCategoriesCollection() { return $this->collectionFactory->create(); }
/** * DataProvider constructor * * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param EavValidationRules $eavValidationRules * @param CategoryCollectionFactory $categoryCollectionFactory * @param StoreManagerInterface $storeManager * @param \Magento\Framework\Registry $registry * @param Config $eavConfig * @param \Magento\Framework\App\RequestInterface $request * @param CategoryFactory $categoryFactory * @param array $meta * @param array $data * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct($name, $primaryFieldName, $requestFieldName, EavValidationRules $eavValidationRules, CategoryCollectionFactory $categoryCollectionFactory, StoreManagerInterface $storeManager, \Magento\Framework\Registry $registry, Config $eavConfig, \Magento\Framework\App\RequestInterface $request, CategoryFactory $categoryFactory, array $meta = [], array $data = []) { $this->eavValidationRules = $eavValidationRules; $this->collection = $categoryCollectionFactory->create(); $this->collection->addAttributeToSelect('*'); $this->eavConfig = $eavConfig; $this->registry = $registry; $this->storeManager = $storeManager; $this->request = $request; $this->categoryFactory = $categoryFactory; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->meta = $this->prepareMeta($this->meta); }