Skip to content

Commit f62f1ee

Browse files
committed
ACP2E-3236: check if both product id and sku are missing before saving the product; added translatable text; added unit test
1 parent 6f4805f commit f62f1ee

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,13 @@ public function save(ProductInterface $product, $saveOptions = false)
526526
$assignToCategories = false;
527527
$tierPrices = $product->getData('tier_price');
528528
$productDataToChange = $product->getData();
529+
530+
if (!$product->getSku() && !$product->getId()) {
531+
throw new \Magento\Framework\Exception\CouldNotSaveException(
532+
__('Invalid product data: %1', 'The SKU or ID of the product must be set before saving the product.')
533+
);
534+
}
535+
529536
try {
530537
$existingProduct = $product->getId() ?
531538
$this->getById($product->getId()) :

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,4 +1568,18 @@ public function testSaveExistingWithMediaGalleryEntries(): void
15681568
$this->model->save($this->product);
15691569
$this->assertEquals($expectedResult, $this->initializedProduct->getMediaGallery('images'));
15701570
}
1571+
1572+
/**
1573+
* @return void
1574+
*/
1575+
public function testSaveCouldNotSaveException(): void
1576+
{
1577+
$this->expectException('Magento\Framework\Exception\CouldNotSaveException');
1578+
$productData = [
1579+
'name' => 'Simple Product',
1580+
'price' => 100
1581+
];
1582+
$this->product->setData($productData);
1583+
$this->model->save($this->product);
1584+
}
15711585
}

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,4 @@ Details,Details
824824
"Restricted admin is allowed to perform actions with images or videos, only when the admin has rights to all websites which the product is assigned to.","Restricted admin is allowed to perform actions with images or videos, only when the admin has rights to all websites which the product is assigned to."
825825
"Synchronize website specific attributes values","Synchronize website specific attributes values"
826826
"The parent product SKU is required for linking child products. Please ensure the parent product SKU is provided and try again.","The parent product SKU is required for linking child products. Please ensure the parent product SKU is provided and try again."
827+
"The SKU or ID of the product must be set before saving the product.","The SKU or ID of the product must be set before saving the product."

0 commit comments

Comments
 (0)