7
7
8
8
namespace Magento \Catalog \Model ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductExtension ;
10
11
use Magento \Catalog \Api \Data \ProductInterface ;
11
12
use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
12
13
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
+ use Magento \Eav \Model \Entity \Attribute \Exception as AttributeException ;
13
15
use Magento \Framework \Api \Data \ImageContentInterface ;
14
16
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
15
17
use Magento \Framework \Api \ImageContentValidatorInterface ;
22
24
use Magento \Framework \Exception \InputException ;
23
25
use Magento \Framework \Exception \LocalizedException ;
24
26
use Magento \Framework \Exception \NoSuchEntityException ;
27
+ use Magento \Framework \Exception \TemporaryState \CouldNotSaveException as TemporaryCouldNotSaveException ;
25
28
use Magento \Framework \Exception \StateException ;
26
29
use Magento \Framework \Exception \ValidatorException ;
27
30
@@ -306,10 +309,10 @@ protected function getCacheKey($data)
306
309
* Add product to internal cache and truncate cache if it has more than cacheLimit elements.
307
310
*
308
311
* @param string $cacheKey
309
- * @param \Magento\Catalog\Api\Data\ ProductInterface $product
312
+ * @param ProductInterface $product
310
313
* @return void
311
314
*/
312
- private function cacheProduct ($ cacheKey , \ Magento \ Catalog \ Api \ Data \ ProductInterface $ product )
315
+ private function cacheProduct ($ cacheKey , ProductInterface $ product )
313
316
{
314
317
$ this ->instancesById [$ product ->getId ()][$ cacheKey ] = $ product ;
315
318
$ this ->saveProductInLocalCache ($ product , $ cacheKey );
@@ -326,7 +329,7 @@ private function cacheProduct($cacheKey, \Magento\Catalog\Api\Data\ProductInterf
326
329
*
327
330
* @param array $productData
328
331
* @param bool $createNew
329
- * @return \Magento\Catalog\Api\Data\ ProductInterface|Product
332
+ * @return ProductInterface|Product
330
333
* @throws NoSuchEntityException
331
334
*/
332
335
protected function initializeProductData (array $ productData , $ createNew )
@@ -414,12 +417,12 @@ protected function processNewMediaGalleryEntry(
414
417
/**
415
418
* Process product links, creating new links, updating and deleting existing links
416
419
*
417
- * @param \Magento\Catalog\Api\Data\ ProductInterface $product
420
+ * @param ProductInterface $product
418
421
* @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $newLinks
419
422
* @return $this
420
423
* @throws NoSuchEntityException
421
424
*/
422
- private function processLinks (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product , $ newLinks )
425
+ private function processLinks (ProductInterface $ product , $ newLinks )
423
426
{
424
427
if ($ newLinks === null ) {
425
428
// If product links were not specified, don't do anything
@@ -548,11 +551,11 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
548
551
}
549
552
550
553
/**
551
- * { @inheritdoc}
554
+ * @inheritdoc
552
555
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
553
556
* @SuppressWarnings(PHPMD.NPathComplexity)
554
557
*/
555
- public function save (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product , $ saveOptions = false )
558
+ public function save (ProductInterface $ product , $ saveOptions = false )
556
559
{
557
560
$ tierPrices = $ product ->getData ('tier_price ' );
558
561
@@ -566,12 +569,18 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
566
569
if (!$ product ->hasData (Product::STATUS )) {
567
570
$ product ->setStatus ($ existingProduct ->getStatus ());
568
571
}
572
+
573
+ /** @var ProductExtension $extensionAttributes */
574
+ $ extensionAttributes = $ product ->getExtensionAttributes ();
575
+ if (empty ($ extensionAttributes ->__toArray ())) {
576
+ $ product ->setExtensionAttributes ($ existingProduct ->getExtensionAttributes ());
577
+ }
569
578
} catch (NoSuchEntityException $ e ) {
570
579
$ existingProduct = null ;
571
580
}
572
581
573
582
$ productDataArray = $ this ->extensibleDataObjectConverter
574
- ->toNestedArray ($ product , [], \ Magento \ Catalog \ Api \ Data \ ProductInterface::class);
583
+ ->toNestedArray ($ product , [], ProductInterface::class);
575
584
$ productDataArray = array_replace ($ productDataArray , $ product ->getData ());
576
585
$ ignoreLinksFlag = $ product ->getData ('ignore_links_flag ' );
577
586
$ productLinks = null ;
@@ -597,47 +606,11 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
597
606
);
598
607
}
599
608
600
- try {
601
- if ($ tierPrices !== null ) {
602
- $ product ->setData ('tier_price ' , $ tierPrices );
603
- }
604
- $ this ->removeProductFromLocalCache ($ product ->getSku ());
605
- unset($ this ->instancesById [$ product ->getId ()]);
606
- $ this ->resourceModel ->save ($ product );
607
- } catch (ConnectionException $ exception ) {
608
- throw new \Magento \Framework \Exception \TemporaryState \CouldNotSaveException (
609
- __ ('Database connection error ' ),
610
- $ exception ,
611
- $ exception ->getCode ()
612
- );
613
- } catch (DeadlockException $ exception ) {
614
- throw new \Magento \Framework \Exception \TemporaryState \CouldNotSaveException (
615
- __ ('Database deadlock found when trying to get lock ' ),
616
- $ exception ,
617
- $ exception ->getCode ()
618
- );
619
- } catch (LockWaitException $ exception ) {
620
- throw new \Magento \Framework \Exception \TemporaryState \CouldNotSaveException (
621
- __ ('Database lock wait timeout exceeded ' ),
622
- $ exception ,
623
- $ exception ->getCode ()
624
- );
625
- } catch (\Magento \Eav \Model \Entity \Attribute \Exception $ exception ) {
626
- throw \Magento \Framework \Exception \InputException::invalidFieldValue (
627
- $ exception ->getAttributeCode (),
628
- $ product ->getData ($ exception ->getAttributeCode ()),
629
- $ exception
630
- );
631
- } catch (ValidatorException $ e ) {
632
- throw new CouldNotSaveException (__ ($ e ->getMessage ()));
633
- } catch (LocalizedException $ e ) {
634
- throw $ e ;
635
- } catch (\Exception $ e ) {
636
- throw new \Magento \Framework \Exception \CouldNotSaveException (
637
- __ ('The product was unable to be saved. Please try again. ' ),
638
- $ e
639
- );
609
+ if ($ tierPrices !== null ) {
610
+ $ product ->setData ('tier_price ' , $ tierPrices );
640
611
}
612
+
613
+ $ this ->saveProduct ($ product );
641
614
$ this ->removeProductFromLocalCache ($ product ->getSku ());
642
615
unset($ this ->instancesById [$ product ->getId ()]);
643
616
@@ -647,7 +620,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
647
620
/**
648
621
* {@inheritdoc}
649
622
*/
650
- public function delete (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product )
623
+ public function delete (ProductInterface $ product )
651
624
{
652
625
$ sku = $ product ->getSku ();
653
626
$ productId = $ product ->getId ();
@@ -862,4 +835,55 @@ private function prepareSku(string $sku): string
862
835
{
863
836
return mb_strtolower (trim ($ sku ));
864
837
}
838
+
839
+ /**
840
+ * Save product resource model.
841
+ *
842
+ * @param ProductInterface|Product $product
843
+ * @throws TemporaryCouldNotSaveException
844
+ * @throws InputException
845
+ * @throws CouldNotSaveException
846
+ * @throws LocalizedException
847
+ */
848
+ private function saveProduct ($ product ): void
849
+ {
850
+ try {
851
+ $ this ->removeProductFromLocalCache ($ product ->getSku ());
852
+ unset($ this ->instancesById [$ product ->getId ()]);
853
+ $ this ->resourceModel ->save ($ product );
854
+ } catch (ConnectionException $ exception ) {
855
+ throw new TemporaryCouldNotSaveException (
856
+ __ ('Database connection error ' ),
857
+ $ exception ,
858
+ $ exception ->getCode ()
859
+ );
860
+ } catch (DeadlockException $ exception ) {
861
+ throw new TemporaryCouldNotSaveException (
862
+ __ ('Database deadlock found when trying to get lock ' ),
863
+ $ exception ,
864
+ $ exception ->getCode ()
865
+ );
866
+ } catch (LockWaitException $ exception ) {
867
+ throw new TemporaryCouldNotSaveException (
868
+ __ ('Database lock wait timeout exceeded ' ),
869
+ $ exception ,
870
+ $ exception ->getCode ()
871
+ );
872
+ } catch (AttributeException $ exception ) {
873
+ throw InputException::invalidFieldValue (
874
+ $ exception ->getAttributeCode (),
875
+ $ product ->getData ($ exception ->getAttributeCode ()),
876
+ $ exception
877
+ );
878
+ } catch (ValidatorException $ e ) {
879
+ throw new CouldNotSaveException (__ ($ e ->getMessage ()));
880
+ } catch (LocalizedException $ e ) {
881
+ throw $ e ;
882
+ } catch (\Exception $ e ) {
883
+ throw new CouldNotSaveException (
884
+ __ ('The product was unable to be saved. Please try again. ' ),
885
+ $ e
886
+ );
887
+ }
888
+ }
865
889
}
0 commit comments