Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position::saveProductPositions PHP Méthode

saveProductPositions() public méthode

Save the product postions.
public saveProductPositions ( Magento\Catalog\Api\Data\CategoryInterface $category ) : Position
$category Magento\Catalog\Api\Data\CategoryInterface Saved category.
Résultat 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;
    }

Usage Example

 /**
  * Resource model save function plugin.
  * Append a commit callback to save the product positions.
  *
  * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource Category original resource model.
  * @param \Closure                                      $proceed          Original save method.
  * @param \Magento\Framework\Model\AbstractModel        $category         Saved category.
  *
  * @return \Magento\Catalog\Model\ResourceModel\Category
  */
 public function aroundSave(\Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Closure $proceed, \Magento\Framework\Model\AbstractModel $category)
 {
     if ($category->getId() && $category->getSortedProducts()) {
         $this->unserializeProductPositions($category);
         if ($category->getIsVirtualCategory()) {
             $category->setPostedProducts([]);
         }
         $categoryResource->addCommitCallback(function () use($category) {
             $affectedProductIds = $this->getAffectedProductIds($category);
             $category->setAffectedProductIds($affectedProductIds);
             $this->saveHandler->saveProductPositions($category);
         });
     }
     return $proceed($category);
 }