0

While unasign the product from multiple categories to single category and trying to save the product, it thorow the error like below

Could not save product "20072" with position 0 to category 7

I am saving the product mannually via admin end

1
  • Thank you for your response, I will the check then i will put tick mark
    – Jaisa
    Commented Jun 7, 2018 at 7:37

3 Answers 3

1

It is fixed in Magento latest version.

You can check fixes here.

6
  • I am using magento 2.2.2
    – Jaisa
    Commented Jun 7, 2018 at 7:38
  • In which version it was fixed
    – Jaisa
    Commented Jun 7, 2018 at 7:38
  • it fixed in 2.2.4 @Sri Commented Jun 7, 2018 at 7:39
  • hi @NikunjVadariya, I am using 2.2.4, but still error is there, Commented Nov 24, 2018 at 11:02
  • Magento 2.2.6 still happens Commented May 6, 2019 at 20:13
5

To fix this issue i created a custom module.

https://github.com/khasru/magento2UrlRewritebug

Hope it helps.

1
  • 1
    after install extension. error show: Fatal error: Declaration of Scriptlodge\UrlRewrite\Model\Storage\DbStorage::doReplace(array $urls) must be compatible with Magento\UrlRewrite\Model\Storage\DbStorage::doReplace(array $urls) . how to fix?
    – huykon225
    Commented Jul 17, 2020 at 10:21
0
public function deleteByIds($categoryId, $sku)
{
    $category = $this->categoryRepository->get($categoryId);
    $product = $this->productRepository->get($sku);
    $productPositions = $category->getProductsPosition();

    $productID = $product->getId();
    if (!isset($productPositions[$productID])) {
        throw new InputException(__('Category does not contain specified product'));
    }
    $backupPosition = $productPositions[$productID];
    unset($productPositions[$productID]);

    $category->setPostedProducts($productPositions);
    try {
        $category->save();
    } catch (\Exception $e) {
        echo $e->getMessage();
        exit;
        throw new CouldNotSaveException(
            __(
                'Could not save product "%product" with position %position to category %category',
                [
                    "product" => $product->getId(),
                    "position" => $backupPosition,
                    "category" => $category->getId()
                ]
            ),
            $e
        );
    }
    return true;
}

the "echo $e->getMessage();" gives you the exact error. In my case it was "Duplication of URL key."

So just check what error message you are getting from here....

File: Vendor/magento/module-catalog/Model/CategoryLinkRepository.php

Not the answer you're looking for? Browse other questions tagged or ask your own question.