Skip to content

Commit 8227c72

Browse files
authored
Product->save() shouldn't be called directly
Here is a problem with calling Product->save() directly: When product has custom options it should have 'has_options' and possibly 'required_options' set to true but calling Product->save() without $product->setCanSaveCustomOptions(true); will result in both 'has_options' and 'required_options' to be set to false. Also calling $product->save() won't invalidate internal ProductRepository's cache (instances , instancesById) and this would produces other unexpected issues.
1 parent 2935aae commit 8227c72

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

app/code/Magento/Catalog/Model/ProductWebsiteLinkRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function save(ProductWebsiteLinkInterface $productWebsiteLink)
3737
$product = $this->productRepository->get($productWebsiteLink->getSku());
3838
$product->setWebsiteIds(array_merge($product->getWebsiteIds(), [$productWebsiteLink->getWebsiteId()]));
3939
try {
40-
$product->save();
40+
$this->productRepository->save($product);
4141
} catch (\Exception $e) {
4242
throw new CouldNotSaveException(
4343
__(
@@ -68,7 +68,7 @@ public function deleteById($sku, $websiteId)
6868
$product->setWebsiteIds(array_diff($product->getWebsiteIds(), [$websiteId]));
6969

7070
try {
71-
$product->save();
71+
$this->productRepository->save($product);
7272
} catch (\Exception $e) {
7373
throw new CouldNotSaveException(
7474
__(

0 commit comments

Comments
 (0)