/** * 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; }
/** * 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; }
/** * Transform a category in query rule. * * @param CategoryInterface $category Category. * * @return QueryInterface */ private function getStandardCategoryQuery(CategoryInterface $category) { $conditionsParams = ['data' => ['attribute' => 'category_ids', 'operator' => '()', 'value' => $category->getId()]]; $categoryCondition = $this->productConditionsFactory->create($conditionsParams); return $this->queryBuilder->getSearchQuery($categoryCondition); }