Skip to content

Commit 8848d94

Browse files
authored
🔃 [EngCom] Public Pull Requests - 2.2-develop
Accepted Public Pull Requests: - #13034: Magento 2.2 Develop fix for #12221 Google Analytics Pageview Triggered twice (by @bhargavmehta) - #13036: #12705: Integrity constraint violation error after re… (by @vinayshah) - #13044: Fix Newsletter Subscribe Workflow (by @torhoehn) - #12668: Fix for reverting stock twice for cancelled orders (by @dverkade) - magento-engcom/magento2ce#1018: 6486: Unable to save certain product properties via Rest API (by @nmalevanec) Fixed GitHub Issues: - #12221: Google analytics pageview being triggered twice (reported by @alexhadley) has been fixed in #13034 by @bhargavmehta in 2.2-develop branch Related commits: 1. 149d08f 2. e851948 - #12705: Integrity constraint violation error after reordering product with custom options (reported by @alena-marchenko) has been fixed in #13036 by @vinayshah in 2.2-develop branch Related commits: 1. 843bc62 2. f89e04d 3. df96b51 - #12876: Multiple newsletter confirmation emails sent (reported by @nfourteen) has been fixed in #13044 by @torhoehn in 2.2-develop branch Related commits: 1. 9ab75ba 2. 93453a8 - #9969: Cancel order and restore quote methods increase stocks twice (reported by @simpleadm) has been fixed in #12668 by @dverkade in 2.2-develop branch Related commits: 1. 2c1d6a4 - #6486: Unable to save certain product properties via Rest API (reported by @cherreman) has been fixed in magento-engcom/magento2ce#1018 by @nmalevanec in 2.2-develop branch Related commits: 1. fa6782f 2. 9db0273 3. 1d37cce 4. de67388 5. d132c7e
2 parents 5bd0104 + 4afdd8b commit 8848d94

File tree

11 files changed

+549
-264
lines changed

11 files changed

+549
-264
lines changed

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

+14-172
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
*/
77
namespace Magento\Catalog\Model;
88

9-
use Magento\Catalog\Api\Data\ProductInterface;
109
use Magento\Catalog\Model\Product\Gallery\MimeTypeExtensionMap;
1110
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12-
use Magento\Framework\Api\Data\ImageContentInterface;
1311
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
1412
use Magento\Framework\Api\ImageContentValidatorInterface;
1513
use Magento\Framework\Api\ImageProcessorInterface;
@@ -18,10 +16,8 @@
1816
use Magento\Framework\DB\Adapter\DeadlockException;
1917
use Magento\Framework\DB\Adapter\LockWaitException;
2018
use Magento\Framework\Exception\CouldNotSaveException;
21-
use Magento\Framework\Exception\InputException;
2219
use Magento\Framework\Exception\LocalizedException;
2320
use Magento\Framework\Exception\NoSuchEntityException;
24-
use Magento\Framework\Exception\StateException;
2521
use Magento\Framework\Exception\ValidatorException;
2622

2723
/**
@@ -116,11 +112,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
116112
protected $fileSystem;
117113

118114
/**
115+
* @deprecated
116+
* @see \Magento\Catalog\Model\MediaGalleryProcessor
119117
* @var ImageContentInterfaceFactory
120118
*/
121119
protected $contentFactory;
122120

123121
/**
122+
* @deprecated
123+
* @see \Magento\Catalog\Model\MediaGalleryProcessor
124124
* @var ImageProcessorInterface
125125
*/
126126
protected $imageProcessor;
@@ -131,7 +131,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
131131
protected $extensionAttributesJoinProcessor;
132132

133133
/**
134-
* @var \Magento\Catalog\Model\Product\Gallery\Processor
134+
* @var ProductRepository\MediaGalleryProcessor
135135
*/
136136
protected $mediaGalleryProcessor;
137137

@@ -329,6 +329,9 @@ protected function initializeProductData(array $productData, $createNew)
329329
unset($productData['media_gallery']);
330330
if ($createNew) {
331331
$product = $this->productFactory->create();
332+
if (isset($productData['price']) && !isset($productData['product_type'])) {
333+
$product->setTypeId(Product\Type::TYPE_SIMPLE);
334+
}
332335
if ($this->storeManager->hasSingleStore()) {
333336
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
334337
}
@@ -375,53 +378,6 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
375378
$product->setWebsiteIds($websiteIds);
376379
}
377380

378-
/**
379-
* @param ProductInterface $product
380-
* @param array $newEntry
381-
* @return $this
382-
* @throws InputException
383-
* @throws StateException
384-
* @throws \Magento\Framework\Exception\LocalizedException
385-
*/
386-
protected function processNewMediaGalleryEntry(
387-
ProductInterface $product,
388-
array $newEntry
389-
) {
390-
/** @var ImageContentInterface $contentDataObject */
391-
$contentDataObject = $newEntry['content'];
392-
393-
/** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
394-
$mediaConfig = $product->getMediaConfig();
395-
$mediaTmpPath = $mediaConfig->getBaseTmpMediaPath();
396-
397-
$relativeFilePath = $this->imageProcessor->processImageContent($mediaTmpPath, $contentDataObject);
398-
$tmpFilePath = $mediaConfig->getTmpMediaShortUrl($relativeFilePath);
399-
400-
if (!$product->hasGalleryAttribute()) {
401-
throw new StateException(__('Requested product does not support images.'));
402-
}
403-
404-
$imageFileUri = $this->getMediaGalleryProcessor()->addImage(
405-
$product,
406-
$tmpFilePath,
407-
isset($newEntry['types']) ? $newEntry['types'] : [],
408-
true,
409-
isset($newEntry['disabled']) ? $newEntry['disabled'] : true
410-
);
411-
// Update additional fields that are still empty after addImage call
412-
$this->getMediaGalleryProcessor()->updateImage(
413-
$product,
414-
$imageFileUri,
415-
[
416-
'label' => $newEntry['label'],
417-
'position' => $newEntry['position'],
418-
'disabled' => $newEntry['disabled'],
419-
'media_type' => $newEntry['media_type'],
420-
]
421-
);
422-
return $this;
423-
}
424-
425381
/**
426382
* Process product links, creating new links, updating and deleting existing links
427383
*
@@ -480,67 +436,6 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
480436
return $this;
481437
}
482438

483-
/**
484-
* Process Media gallery data before save product.
485-
*
486-
* Compare Media Gallery Entries Data with existing Media Gallery
487-
* * If Media entry has not value_id set it as new
488-
* * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
489-
* * Merge Existing and new media gallery
490-
*
491-
* @param ProductInterface $product contains only existing media gallery items
492-
* @param array $mediaGalleryEntries array which contains all media gallery items
493-
* @return $this
494-
* @throws InputException
495-
* @throws StateException
496-
* @throws LocalizedException
497-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
498-
*/
499-
protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
500-
{
501-
$existingMediaGallery = $product->getMediaGallery('images');
502-
$newEntries = [];
503-
$entriesById = [];
504-
if (!empty($existingMediaGallery)) {
505-
foreach ($mediaGalleryEntries as $entry) {
506-
if (isset($entry['id'])) {
507-
$entriesById[$entry['id']] = $entry;
508-
} else {
509-
$newEntries[] = $entry;
510-
}
511-
}
512-
foreach ($existingMediaGallery as $key => &$existingEntry) {
513-
if (isset($entriesById[$existingEntry['value_id']])) {
514-
$updatedEntry = $entriesById[$existingEntry['value_id']];
515-
if (array_key_exists('file', $updatedEntry) && $updatedEntry['file'] === null) {
516-
unset($updatedEntry['file']);
517-
}
518-
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
519-
} else {
520-
//set the removed flag
521-
$existingEntry['removed'] = true;
522-
}
523-
}
524-
unset($existingEntry);
525-
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
526-
} else {
527-
$newEntries = $mediaGalleryEntries;
528-
}
529-
530-
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
531-
$images = $product->getMediaGallery('images');
532-
if ($images) {
533-
foreach ($images as $image) {
534-
if (!isset($image['removed']) && !empty($image['types'])) {
535-
$this->getMediaGalleryProcessor()->setMediaAttribute($product, $image['types'], $image['file']);
536-
}
537-
}
538-
}
539-
$this->processEntries($product, $newEntries, $entriesById);
540-
541-
return $this;
542-
}
543-
544439
/**
545440
* {@inheritdoc}
546441
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -577,7 +472,10 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
577472

578473
$this->processLinks($product, $productLinks);
579474
if (isset($productDataArray['media_gallery_entries'])) {
580-
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
475+
$this->getMediaGalleryProcessor()->processMediaGallery(
476+
$product,
477+
$productDataArray['media_gallery_entries']
478+
);
581479
}
582480

583481
if (!$product->getOptionsReadonly()) {
@@ -749,13 +647,13 @@ public function cleanCache()
749647
}
750648

751649
/**
752-
* @return Product\Gallery\Processor
650+
* @return ProductRepository\MediaGalleryProcessor
753651
*/
754652
private function getMediaGalleryProcessor()
755653
{
756654
if (null === $this->mediaGalleryProcessor) {
757655
$this->mediaGalleryProcessor = \Magento\Framework\App\ObjectManager::getInstance()
758-
->get(\Magento\Catalog\Model\Product\Gallery\Processor::class);
656+
->get(ProductRepository\MediaGalleryProcessor::class);
759657
}
760658
return $this->mediaGalleryProcessor;
761659
}
@@ -775,60 +673,4 @@ private function getCollectionProcessor()
775673
}
776674
return $this->collectionProcessor;
777675
}
778-
779-
/**
780-
* Convert extension attribute for product media gallery.
781-
*
782-
* @param array $newEntry
783-
* @param array $extensionAttributes
784-
* @return void
785-
*/
786-
private function processExtensionAttributes(array &$newEntry, array $extensionAttributes)
787-
{
788-
foreach ($extensionAttributes as $code => $value) {
789-
if (is_array($value)) {
790-
$this->processExtensionAttributes($newEntry, $value);
791-
} else {
792-
$newEntry[$code] = $value;
793-
}
794-
}
795-
unset($newEntry['extension_attributes']);
796-
}
797-
798-
/**
799-
* Convert entries into product media gallery data and set to product.
800-
*
801-
* @param ProductInterface $product
802-
* @param array $newEntries
803-
* @param array $entriesById
804-
* @throws InputException
805-
* @throws LocalizedException
806-
* @throws StateException
807-
* @return void
808-
*/
809-
private function processEntries(ProductInterface $product, array $newEntries, array $entriesById)
810-
{
811-
foreach ($newEntries as $newEntry) {
812-
if (!isset($newEntry['content'])) {
813-
throw new InputException(__('The image content is not valid.'));
814-
}
815-
/** @var ImageContentInterface $contentDataObject */
816-
$contentDataObject = $this->contentFactory->create()
817-
->setName($newEntry['content'][ImageContentInterface::NAME])
818-
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
819-
->setType($newEntry['content'][ImageContentInterface::TYPE]);
820-
$newEntry['content'] = $contentDataObject;
821-
$this->processNewMediaGalleryEntry($product, $newEntry);
822-
823-
$finalGallery = $product->getData('media_gallery');
824-
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
825-
if (isset($newEntry['extension_attributes'])) {
826-
$this->processExtensionAttributes($newEntry, $newEntry['extension_attributes']);
827-
}
828-
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
829-
$entriesById[$newEntryId] = $newEntry;
830-
$finalGallery['images'][$newEntryId] = $newEntry;
831-
$product->setData('media_gallery', $finalGallery);
832-
}
833-
}
834676
}

0 commit comments

Comments
 (0)