/** * Save the product postions. * * @param CategoryInterface $category Saved category. * * @return \Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position */ public function saveProductPositions(CategoryInterface $category) { $newProductPositions = $category->getSortedProducts(); $deleteConditions = [$this->getConnection()->quoteInto('category_id = ?', (int) $category->getId())]; if (!empty($newProductPositions)) { $insertData = []; foreach ($newProductPositions as $productId => $position) { $insertData[] = ['category_id' => $category->getId(), 'product_id' => $productId, 'position' => $position]; } $deleteConditions[] = $this->getConnection()->quoteInto('product_id NOT IN (?)', array_keys($newProductPositions)); $this->getConnection()->insertOnDuplicate($this->getMainTable(), $insertData, array_keys(current($insertData))); } $this->getConnection()->delete($this->getMainTable(), implode(' AND ', $deleteConditions)); return $this; }
/** * Build category URL path * * @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category * @return string */ public function getUrlPath($category) { if ($category->getParentId() == Category::TREE_ROOT_ID) { return ''; } $path = $category->getUrlPath(); if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) { return $path; } $path = $category->getUrlKey(); if ($path === false) { return $category->getUrlPath(); } if ($this->isNeedToGenerateUrlPathForParent($category)) { $parentPath = $this->getUrlPath($this->categoryRepository->get($category->getParentId())); $path = $parentPath === '' ? $path : $parentPath . '/' . $path; } return $path; }
/** * Return the filter applied to the query. * * @SuppressWarnings(PHPMD.ElseExpression) * * @return QueryInterface */ private function getQueryFilter() { $queryParams = []; $this->category->setIsActive(true); if ($this->category->getIsVirtualCategory() || $this->category->getId()) { $queryParams['must'][] = $this->category->getVirtualRule()->getCategorySearchQuery($this->category); } elseif (!$this->category->getId()) { $queryParams['must'][] = $this->getEntityIdFilterQuery([0]); } if ((bool) $this->category->getIsVirtualCategory() === false) { $addedProductIds = $this->category->getAddedProductIds(); $deletedProductIds = $this->category->getDeletedProductIds(); if ($addedProductIds && !empty($addedProductIds)) { $queryParams = ['should' => $queryParams['must']]; $queryParams['should'][] = $this->getEntityIdFilterQuery($addedProductIds); } if ($deletedProductIds && !empty($deletedProductIds)) { $queryParams['mustNot'][] = $this->getEntityIdFilterQuery($deletedProductIds); } } return $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams); }
/** * {@inheritdoc} */ public function delete(\Magento\Catalog\Api\Data\CategoryInterface $category) { try { $categoryId = $category->getId(); $this->categoryResource->delete($category); } catch (\Exception $e) { throw new StateException(__('Cannot delete category with id %1', $category->getId()), $e); } unset($this->instances[$categoryId]); return true; }
/** * Returns the list of the virtual categories available under a category. * * @param CategoryInterface $category Category. * @param array $excludedCategories Category already used into the building stack. Avoid short circuit. * * @return \Magento\Catalog\Model\ResourceModel\Category\Collection; */ private function getChildrenVirtualCategories(CategoryInterface $category, $excludedCategories = []) { $storeId = $category->getStoreId(); $categoryCollection = $this->categoryCollectionFactory->create()->setStoreId($storeId); $categoryCollection->addAttributeToFilter('is_virtual_category', 1)->addAttributeToFilter('is_active', ['neq' => 0])->addPathFilter(sprintf('%s/.*', $category->getPath())); if (!empty($excludedCategories)) { $categoryCollection->addAttributeToFilter('entity_id', ['nin' => $excludedCategories]); } $categoryCollection->addAttributeToSelect(['is_active', 'virtual_category_root', 'is_virtual_category', 'virtual_rule']); return $categoryCollection; }
/** * Append products sorted by the user to the category. * * @param CategoryInterface $category Category. * * @return $this */ private function setSortedProducts(CategoryInterface $category) { $productPositions = $this->getRequest()->getParam('product_position', []); $category->setSortedProductIds(array_keys($productPositions)); return $this; }