/** * 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; }