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);
    }
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);
    }
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 ) ; }
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
Resource model save function plugin . Append a commit callback to save the product positions .