/**
  * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url
  * @param int $storeId
  * @return array
  */
 protected function generateForCustom($url, $storeId)
 {
     $urls = [];
     $targetPath = !$url->getRedirectType() ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId);
     if ($url->getRequestPath() !== $targetPath) {
         $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->category->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType($url->getRedirectType())->setStoreId($storeId)->setDescription($url->getDescription())->setIsAutogenerated(0)->setMetadata($url->getMetadata());
     }
     return $urls;
 }
 /**
  * Generate list of urls for global scope
  *
  * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
  */
 protected function generateForGlobalScope()
 {
     $urls = [];
     $categoryId = $this->category->getId();
     foreach ($this->category->getStoreIds() as $id) {
         if (!$this->isGlobalScope($id) && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $categoryId, Category::ENTITY)) {
             $urls = array_merge($urls, $this->generateForSpecificStoreView($id));
         }
     }
     return $urls;
 }
 /**
  * Creates a new child category for $parentCategory
  *
  * @param \Magento\Catalog\Model\Category $parentCategory
  * @return \Magento\Catalog\Model\Category
  */
 protected function createCategory($parentCategory)
 {
     /** @var \Magento\Catalog\Model\Category $category */
     $category = $this->objectManager->create('Magento\\Catalog\\Model\\Category');
     $category->setStoreId(self::DEFAULT_STORE_ID)->setParentId($parentCategory->getId())->setName(self::NAMES_PREFIX . $this->titlesGenerator->generateCategoryTitle())->setAttributeSetId($category->getDefaultAttributeSetId())->setLevel($parentCategory->getLevel() + 1)->setPath($parentCategory->getPath())->setIsActive(1)->save();
     return $category;
 }
Example #4
0
 public function testCheckId()
 {
     $this->_model = $this->getCategoryByName('Category 1.1.1');
     $categoryId = $this->_model->getId();
     $this->assertEquals($categoryId, $this->_model->checkId($categoryId));
     $this->assertFalse($this->_model->checkId(111));
 }
Example #5
0
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @return Node
  */
 protected function getNode(\Magento\Catalog\Model\Category $category)
 {
     $nodeId = $category->getId();
     $node = $this->categoryTree->loadNode($nodeId);
     $node->loadChildren();
     $this->prepareCollection();
     $this->categoryTree->addCollectionData($this->categoryCollection);
     return $node;
 }
 /**
  * Reindex category's data after into search engine after reindexing the category
  *
  * @param \Magento\Catalog\Model\Category $subject The category being reindexed
  * @param callable                        $proceed The parent function we are plugged on
  *                                                 : Magento\Catalog\Model\Category::reindex()
  *
  * @return \Magento\Catalog\Model\Category
  */
 public function aroundReindex(\Magento\Catalog\Model\Category $subject, callable $proceed)
 {
     $proceed();
     if ($subject->getLevel() > 1) {
         $categoryIndexer = $this->indexerRegistry->get(\Smile\ElasticsuiteCatalog\Model\Category\Indexer\Fulltext::INDEXER_ID);
         if (!$categoryIndexer->isScheduled()) {
             $categoryIndexer->reindexRow($subject->getId());
         }
     }
     return;
 }
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @param boolean $recursive
  * @return int[]
  */
 public function getChildrenIds(Category $category, $recursive = false)
 {
     $cacheKey = $category->getId() . '_' . (int) $recursive;
     if (!isset($this->childrenIds[$cacheKey])) {
         $connection = $category->getResource()->getReadConnection();
         $select = $connection->select()->from($category->getResource()->getEntityTable(), 'entity_id')->where($connection->quoteIdentifier('path') . ' LIKE :c_path');
         $bind = ['c_path' => $category->getPath() . '/%'];
         if (!$recursive) {
             $select->where($connection->quoteIdentifier('level') . ' <= :c_level');
             $bind['c_level'] = $category->getLevel() + 1;
         }
         $this->childrenIds[$cacheKey] = $connection->fetchCol($select, $bind);
     }
     return $this->childrenIds[$cacheKey];
 }
 /**
  * @magentoAppArea adminhtml
  */
 public function testDeleteChildren()
 {
     $this->_model->unsetData();
     $this->_model->load(4);
     $this->_model->setSkipDeleteChildren(true);
     $this->_model->delete();
     $this->_model->unsetData();
     $this->_model->load(5);
     $this->assertEquals($this->_model->getId(), 5);
     $this->_model->unsetData();
     $this->_model->load(3);
     $this->assertEquals($this->_model->getId(), 3);
     $this->_model->delete();
     $this->_model->unsetData();
     $this->_model->load(5);
     $this->assertEquals($this->_model->getId(), null);
 }
 /**
  * Get canonical product url path
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Catalog\Model\Category|null $category
  * @return string
  */
 public function getCanonicalUrlPath($product, $category = null)
 {
     $path = 'catalog/product/view/id/' . $product->getId();
     return $category ? $path . '/category/' . $category->getId() : $path;
 }
Example #10
0
 /**
  * Get products count in category
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return integer
  */
 public function getProductCount($category)
 {
     $select = $this->_getReadAdapter()->select()->from($this->getTable('catalog_category_product'), "COUNT({$this->getTable('catalog_category_product')}.product_id)")->where("{$this->getTable('catalog_category_product')}.category_id = ?", $category->getId())->group("{$this->getTable('catalog_category_product')}.category_id");
     return (int) $this->_getReadAdapter()->fetchOne($select);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function getId()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getId');
     if (!$pluginInfo) {
         return parent::getId();
     } else {
         return $this->___callPlugins('getId', func_get_args(), $pluginInfo);
     }
 }
Example #12
0
 /**
  * Check EAV and flat data
  *
  * @param \Magento\Catalog\Model\Category $category
  */
 protected function checkCategoryData(\Magento\Catalog\Model\Category $category)
 {
     foreach (self::$attributeCodes as $attributeCode) {
         $this->assertEquals(self::$attributeValues[$category->getId()][$attributeCode], $category->getData($attributeCode), "Data for {$category->getId()} attribute code [{$attributeCode}] is wrong");
     }
 }
 /**
  * Get current category
  *
  * @param \Magento\Catalog\Model\Category $category
  *
  * @return Category
  */
 public function isActive($category)
 {
     $activeCategory = $this->_coreRegistry->registry('current_category');
     $activeProduct = $this->_coreRegistry->registry('current_product');
     if (!$activeCategory) {
         // Check if we're on a product page
         if ($activeProduct !== null) {
             return in_array($category->getId(), $activeProduct->getCategoryIds());
         }
         return false;
     }
     // Check if this is the active category
     if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() and $category->getId() == $activeCategory->getId()) {
         return true;
     }
     // Check if a subcategory of this category is active
     $childrenIds = $category->getAllChildren(true);
     if (!is_null($childrenIds) and in_array($activeCategory->getId(), $childrenIds)) {
         return true;
     }
     // Fallback - If Flat categories is not enabled the active category does not give an id
     return $category->getName() == $activeCategory->getName() ? true : false;
 }
Example #14
0
 /**
  * Process positions of old parent category children and new parent category children.
  * Get position for moved category
  *
  * @param \Magento\Catalog\Model\Category $category
  * @param \Magento\Catalog\Model\Category $newParent
  * @param null|int $afterCategoryId
  * @return int
  */
 protected function _processPositions($category, $newParent, $afterCategoryId)
 {
     $table = $this->getEntityTable();
     $adapter = $this->_getWriteAdapter();
     $positionField = $adapter->quoteIdentifier('position');
     $bind = array('position' => new \Zend_Db_Expr($positionField . ' - 1'));
     $where = array('parent_id = ?' => $category->getParentId(), $positionField . ' > ?' => $category->getPosition());
     $adapter->update($table, $bind, $where);
     /**
      * Prepare position value
      */
     if ($afterCategoryId) {
         $select = $adapter->select()->from($table, 'position')->where('entity_id = :entity_id');
         $position = $adapter->fetchOne($select, array('entity_id' => $afterCategoryId));
         $position += 1;
     } else {
         $position = 1;
     }
     $bind = array('position' => new \Zend_Db_Expr($positionField . ' + 1'));
     $where = array('parent_id = ?' => $newParent->getId(), $positionField . ' >= ?' => $position);
     $adapter->update($table, $bind, $where);
     return $position;
 }
 /**
  * Get canonical category url
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return string
  */
 public function getCanonicalUrlPath($category)
 {
     return 'catalog/category/view/id/' . $category->getId();
 }
Example #16
0
 /**
  * Build state key
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return string
  */
 public function toString($category)
 {
     return 'STORE_' . $this->storeManager->getStore()->getId() . '_CAT_' . $category->getId() . '_CUSTGROUP_' . $this->customerSession->getCustomerGroupId();
 }
Example #17
0
 /**
  * Get data for saving code model
  *
  * @return array
  */
 protected function _getCodeData()
 {
     return ['entity_type' => \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_CATEGORY, 'entity_id' => $this->_category->getId(), 'store_id' => $this->_category->getStoreId(), 'experiment_script' => $this->_params['experiment_script']];
 }
Example #18
0
 /**
  * @param Category $category
  * @return Metadata
  */
 public function createDataFromModel(Category $category)
 {
     $builder = $this->builder->populateWithArray($category->getData())->setCategoryId($category->getId())->setActive($category->getIsActive())->setChildren($category->getAllChildren(true));
     return $builder->create();
 }
Example #19
0
 /**
  * Check is root category
  *
  * @param Category $category
  * @return string
  */
 public function isRootCategory(Category $category)
 {
     $store = $this->storeManager->getStore($category->getStoreId());
     return $category->getId() == $store->getRootCategoryId();
 }
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @param int $storeId
  * @return bool
  */
 protected function isCategoryProperForGenerating($category, $storeId)
 {
     if (isset($this->acceptableCategories[$storeId]) && isset($this->acceptableCategories[$storeId][$category->getId()])) {
         return $this->acceptableCategories[$storeId][$category->getId()];
     }
     $acceptable = false;
     if ($category->getParentId() != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
         list(, $rootCategoryId) = $category->getParentIds();
         $acceptable = $rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId();
     }
     if (!isset($this->acceptableCategories[$storeId])) {
         $this->acceptableCategories[$storeId] = [];
     }
     $this->acceptableCategories[$storeId][$category->getId()] = $acceptable;
     return $acceptable;
 }
 /**
  * Filter product collection
  *
  * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
  * @param \Magento\Catalog\Model\Category $category
  * @return void
  */
 public function filter($collection, \Magento\Catalog\Model\Category $category)
 {
     $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addUrlRewrite($category->getId())->setVisibility($this->productVisibility->getVisibleInCatalogIds());
 }
 public function getObject(Category $category)
 {
     /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
     $productCollection = $category->getProductCollection();
     $productCollection = $productCollection->addMinimalPrice();
     $category->setProductCount($productCollection->getSize());
     $transport = new DataObject();
     $this->eventManager->dispatch('algolia_category_index_before', ['category' => $category, 'custom_data' => $transport]);
     $customData = $transport->getData();
     $storeId = $category->getStoreId();
     $category->getUrlInstance()->setStore($storeId);
     $path = '';
     foreach ($category->getPathIds() as $categoryId) {
         if ($path != '') {
             $path .= ' / ';
         }
         $path .= $this->getCategoryName($categoryId, $storeId);
     }
     $image_url = null;
     try {
         $image_url = $category->getImageUrl();
     } catch (\Exception $e) {
         /* no image, no default: not fatal */
     }
     $data = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['category'], 'popularity' => 1, 'product_count' => $category->getProductCount()];
     if (!empty($image_url)) {
         $data['image_url'] = $image_url;
     }
     foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) {
         $value = $category->getData($attribute['attribute']);
         $attribute_resource = $category->getResource()->getAttribute($attribute['attribute']);
         if ($attribute_resource) {
             $value = $attribute_resource->getFrontend()->getValue($category);
         }
         if (isset($data[$attribute['attribute']])) {
             $value = $data[$attribute['attribute']];
         }
         if ($value) {
             $data[$attribute['attribute']] = $value;
         }
     }
     $data = array_merge($data, $customData);
     foreach ($data as &$data0) {
         $data0 = $this->try_cast($data0);
     }
     return $data;
 }
Example #23
0
 /**
  * Validate category for be using as filter
  *
  * @param  \Magento\Catalog\Model\Category $category
  * @return mixed
  */
 protected function _isValidCategory($category)
 {
     if ($category->getId()) {
         while ($category->getLevel() != 0) {
             if (!$category->getIsActive()) {
                 return false;
             }
             $category = $category->getParentCategory();
         }
         return true;
     }
     return false;
 }
 /**
  * Convert category to array
  *
  * @param \Magento\Catalog\Model\Category $category
  * @param \Magento\Catalog\Model\Category $currentCategory
  * @return array
  */
 private function getCategoryAsArray($category, $currentCategory)
 {
     return ['name' => $category->getName(), 'id' => 'category-node-' . $category->getId(), 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => in_array((string) $category->getId(), explode('/', $currentCategory->getPath()), true), 'is_active' => $category->getId() == $currentCategory->getId()];
 }
Example #25
0
 /**
  * Create url rewrite object
  *
  * @param int $storeId
  * @param string $requestPath
  * @param string $targetPath
  * @param string|null $redirectType Null or one of OptionProvider const
  * @return UrlRewrite
  */
 protected function createUrlRewrite($storeId, $requestPath, $targetPath, $redirectType = null)
 {
     return $this->catalogUrlRewriteHelper->createUrlRewrite(self::ENTITY_TYPE_CATEGORY, $this->category->getId(), $storeId, $requestPath, $targetPath, $redirectType);
 }
Example #26
0
 /**
  * Get positions of associated to category products
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return array
  */
 public function getProductsPosition($category)
 {
     $select = $this->getConnection()->select()->from($this->getTable('catalog_category_product'), ['product_id', 'position'])->where('category_id = :category_id');
     $bind = ['category_id' => (int) $category->getId()];
     return $this->getConnection()->fetchPairs($select, $bind);
 }
 /**
  * Specify category filter for product collection
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return $this
  */
 public function addCategoryFilter(\Magento\Catalog\Model\Category $category)
 {
     $this->addFieldToFilter('category_ids', $category->getId());
     return parent::addCategoryFilter($category);
 }
 /**
  * Override url key and url path for category in specific Store
  *
  * @param int $storeId
  * @return void
  */
 protected function updateCategoryUrlForStore($storeId)
 {
     $category = $this->categoryRepository->get($this->category->getId(), $storeId);
     $this->category->addData(['url_key' => $category->getUrlKey(), 'url_path' => $category->getUrlPath()]);
 }
Example #29
0
 /**
  * Specify category filter for product collection
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return $this
  */
 public function addCategoryFilter(\Magento\Catalog\Model\Category $category)
 {
     $this->_productLimitationFilters['category_id'] = $category->getId();
     if ($category->getIsAnchor()) {
         unset($this->_productLimitationFilters['category_is_anchor']);
     } else {
         $this->_productLimitationFilters['category_is_anchor'] = 1;
     }
     if ($this->getStoreId() == Store::DEFAULT_STORE_ID) {
         $this->_applyZeroStoreProductLimitations();
     } else {
         $this->_applyProductLimitations();
     }
     return $this;
 }
 /**
  * Load product saved positions for the current category.
  *
  * @param Category $category Category.
  *
  * @return array
  */
 private function getProductSavedPositions(Category $category)
 {
     $productPositions = [];
     if ($category->getId()) {
         $productPositions = $this->productPositionResource->getProductPositionsByCategory($category);
     }
     return json_encode($productPositions, JSON_FORCE_OBJECT);
 }