Skip to content

Commit 3302864

Browse files
authored
Merge pull request #1368 from magento-tsg/2.1.8-develop-pr24
[TSG] Backporting for 2.1 (pr24) (2.1.8)
2 parents 0987a02 + 57a7aab commit 3302864

File tree

68 files changed

+1555
-1079
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1555
-1079
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ class Helper
7474
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
7575
*/
7676
private $dateTimeFilter;
77-
78-
/**
79-
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
80-
*/
81-
private $linkTypeProvider;
8277

8378
/**
8479
* Helper constructor.
@@ -88,25 +83,21 @@ class Helper
8883
* @param ProductLinks $productLinks
8984
* @param \Magento\Backend\Helper\Js $jsHelper
9085
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
91-
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
9286
*/
9387
public function __construct(
9488
\Magento\Framework\App\RequestInterface $request,
9589
\Magento\Store\Model\StoreManagerInterface $storeManager,
9690
StockDataFilter $stockFilter,
9791
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $productLinks,
9892
\Magento\Backend\Helper\Js $jsHelper,
99-
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
100-
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider = null
93+
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
10194
) {
10295
$this->request = $request;
10396
$this->storeManager = $storeManager;
10497
$this->stockFilter = $stockFilter;
10598
$this->productLinks = $productLinks;
10699
$this->jsHelper = $jsHelper;
107100
$this->dateFilter = $dateFilter;
108-
$this->linkTypeProvider = $linkTypeProvider ?: \Magento\Framework\App\ObjectManager::getInstance()
109-
->get(\Magento\Catalog\Model\Product\LinkTypeProvider::class);
110101
}
111102

112103
/**
@@ -208,18 +199,14 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
208199
$customOptions = [];
209200
foreach ($options as $customOptionData) {
210201
if (empty($customOptionData['is_delete'])) {
211-
if (empty($customOptionData['option_id'])) {
212-
$customOptionData['option_id'] = null;
213-
}
214-
215202
if (isset($customOptionData['values'])) {
216203
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
217204
return empty($valueData['is_delete']);
218205
});
219206
}
220-
221207
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
222208
$customOption->setProductSku($product->getSku());
209+
$customOption->setOptionId(null);
223210
$customOptions[] = $customOption;
224211
}
225212
}
@@ -260,17 +247,11 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
260247

261248
$product = $this->productLinks->initializeLinks($product, $links);
262249
$productLinks = $product->getProductLinks();
263-
$linkTypes = [];
264-
265-
/** @var \Magento\Catalog\Api\Data\ProductLinkTypeInterface $linkTypeObject */
266-
foreach ($this->linkTypeProvider->getItems() as $linkTypeObject) {
267-
$linkTypes[$linkTypeObject->getName()] = $product->getData($linkTypeObject->getName() . '_readonly');
268-
}
269-
270-
// skip linkTypes that were already processed on initializeLinks plugins
271-
foreach ($productLinks as $productLink) {
272-
unset($linkTypes[$productLink->getLinkType()]);
273-
}
250+
$linkTypes = [
251+
'related' => $product->getRelatedReadonly(),
252+
'upsell' => $product->getUpsellReadonly(),
253+
'crosssell' => $product->getCrosssellReadonly()
254+
];
274255

275256
foreach ($linkTypes as $linkType => $readonly) {
276257
if (isset($links[$linkType]) && !$readonly) {
@@ -334,59 +315,21 @@ public function mergeProductOptions($productOptions, $overwriteOptions)
334315
return $productOptions;
335316
}
336317

337-
foreach ($productOptions as $optionIndex => $option) {
318+
foreach ($productOptions as $index => $option) {
338319
$optionId = $option['option_id'];
339-
$option = $this->overwriteValue(
340-
$optionId,
341-
$option,
342-
$overwriteOptions
343-
);
344320

345-
if (isset($option['values']) && isset($overwriteOptions[$optionId]['values'])) {
346-
foreach ($option['values'] as $valueIndex => $value) {
347-
if (isset($value['option_type_id'])) {
348-
$valueId = $value['option_type_id'];
349-
$value = $this->overwriteValue(
350-
$valueId,
351-
$value,
352-
$overwriteOptions[$optionId]['values']
353-
);
354-
355-
$option['values'][$valueIndex] = $value;
356-
}
357-
}
321+
if (!isset($overwriteOptions[$optionId])) {
322+
continue;
358323
}
359324

360-
$productOptions[$optionIndex] = $option;
361-
}
362-
363-
return $productOptions;
364-
}
365-
366-
/**
367-
* Overwrite values of fields to default, if there are option id and field
368-
* name in array overwriteOptions.
369-
*
370-
* @param int $optionId
371-
* @param array $option
372-
* @param array $overwriteOptions
373-
*
374-
* @return array
375-
*/
376-
private function overwriteValue($optionId, $option, $overwriteOptions)
377-
{
378-
if (isset($overwriteOptions[$optionId])) {
379325
foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
380326
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
381-
$option[$fieldName] = $option['default_' . $fieldName];
382-
if ('title' == $fieldName) {
383-
$option['is_delete_store_title'] = 1;
384-
}
327+
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
385328
}
386329
}
387330
}
388331

389-
return $option;
332+
return $productOptions;
390333
}
391334

392335
/**
@@ -396,9 +339,8 @@ private function getCustomOptionFactory()
396339
{
397340
if (null === $this->customOptionFactory) {
398341
$this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
399-
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
342+
->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
400343
}
401-
402344
return $this->customOptionFactory;
403345
}
404346

@@ -409,9 +351,8 @@ private function getProductLinkFactory()
409351
{
410352
if (null === $this->productLinkFactory) {
411353
$this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance()
412-
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
354+
->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory');
413355
}
414-
415356
return $this->productLinkFactory;
416357
}
417358

@@ -422,9 +363,8 @@ private function getProductRepository()
422363
{
423364
if (null === $this->productRepository) {
424365
$this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
425-
->get('\Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
366+
->get('Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
426367
}
427-
428368
return $this->productRepository;
429369
}
430370

@@ -437,7 +377,6 @@ private function getLinkResolver()
437377
if (!is_object($this->linkResolver)) {
438378
$this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
439379
}
440-
441380
return $this->linkResolver;
442381
}
443382

@@ -452,7 +391,6 @@ private function getDateTimeFilter()
452391
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
453392
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
454393
}
455-
456394
return $this->dateTimeFilter;
457395
}
458396
}

app/code/Magento/Catalog/Model/Product/Option/Repository.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -133,47 +133,15 @@ public function duplicate(
133133
*/
134134
public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option)
135135
{
136-
/** @var string $productSku */
137136
$productSku = $option->getProductSku();
138137
if (!$productSku) {
139138
throw new CouldNotSaveException(__('ProductSku should be specified'));
140139
}
141-
/** @var \Magento\Catalog\Model\Product $product */
142140
$product = $this->productRepository->get($productSku);
143-
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
144141
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
145142
$option->setData('product_id', $product->getData($metadata->getLinkField()));
146-
$option->setData('store_id', $product->getStoreId());
147-
148-
if ($option->getOptionId()) {
149-
$options = $product->getOptions();
150-
if (!$options) {
151-
$options = $this->getProductOptions($product);
152-
}
153-
$persistedOption = array_filter(
154-
$options,
155-
function ($iOption) use ($option) {
156-
return $option->getOptionId() == $iOption->getOptionId();
157-
}
158-
);
159-
$persistedOption = reset($persistedOption);
160-
161-
if (!$persistedOption) {
162-
throw new NoSuchEntityException();
163-
}
164-
165-
/** @var array $originalValues */
166-
$originalValues = $persistedOption->getValues();
167-
/** @var array $newValues */
168-
$newValues = $option->getData('values');
169-
if ($newValues) {
170-
$newValues = $this->markRemovedValues($newValues, $originalValues);
171-
$option->setData('values', $newValues);
172-
}
173-
}
174-
143+
$option->setOptionId(null);
175144
$option->save();
176-
177145
return $option;
178146
}
179147

@@ -235,7 +203,7 @@ private function getOptionFactory()
235203
{
236204
if (null === $this->optionFactory) {
237205
$this->optionFactory = \Magento\Framework\App\ObjectManager::getInstance()
238-
->get(\Magento\Catalog\Model\Product\OptionFactory::class);
206+
->get('Magento\Catalog\Model\Product\OptionFactory');
239207
}
240208
return $this->optionFactory;
241209
}
@@ -248,7 +216,7 @@ private function getCollectionFactory()
248216
{
249217
if (null === $this->collectionFactory) {
250218
$this->collectionFactory = \Magento\Framework\App\ObjectManager::getInstance()
251-
->get(\Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory::class);
219+
->get('Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory');
252220
}
253221
return $this->collectionFactory;
254222
}
@@ -261,7 +229,7 @@ private function getMetadataPool()
261229
{
262230
if (null === $this->metadataPool) {
263231
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
264-
->get(\Magento\Framework\EntityManager\MetadataPool::class);
232+
->get('Magento\Framework\EntityManager\MetadataPool');
265233
}
266234
return $this->metadataPool;
267235
}
@@ -274,7 +242,7 @@ private function getHydratorPool()
274242
{
275243
if (null === $this->hydratorPool) {
276244
$this->hydratorPool = \Magento\Framework\App\ObjectManager::getInstance()
277-
->get(\Magento\Framework\EntityManager\HydratorPool::class);
245+
->get('Magento\Framework\EntityManager\HydratorPool');
278246
}
279247
return $this->hydratorPool;
280248
}

app/code/Magento/Catalog/Model/Product/Option/SaveHandler.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SaveHandler implements ExtensionInterface
2020

2121
/**
2222
* @param OptionRepository $optionRepository
23+
* @param MetadataPool $metadataPool
2324
*/
2425
public function __construct(
2526
OptionRepository $optionRepository
@@ -35,32 +36,15 @@ public function __construct(
3536
*/
3637
public function execute($entity, $arguments = [])
3738
{
38-
$options = $entity->getOptions();
39-
$optionIds = [];
40-
41-
if ($options) {
42-
$optionIds = array_map(
43-
function ($option) {
44-
/** @var \Magento\Catalog\Model\Product\Option $option */
45-
return $option->getOptionId();
46-
},
47-
$options
48-
);
49-
}
50-
5139
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
5240
foreach ($this->optionRepository->getProductOptions($entity) as $option) {
53-
if (!in_array($option->getOptionId(), $optionIds)) {
54-
$this->optionRepository->delete($option);
55-
}
41+
$this->optionRepository->delete($option);
5642
}
57-
58-
if ($options) {
59-
foreach ($options as $option) {
43+
if ($entity->getOptions()) {
44+
foreach ($entity->getOptions() as $option) {
6045
$this->optionRepository->save($option);
6146
}
6247
}
63-
6448
return $entity;
6549
}
6650
}

app/code/Magento/Catalog/Model/Product/Option/Validator/Select.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ protected function validateOptionValue(Option $option)
5151
$storeId = $option->getProduct()->getStoreId();
5252
}
5353
foreach ($values as $value) {
54-
if (isset($value['is_delete']) && (bool)$value['is_delete']) {
55-
continue;
56-
}
5754
$type = isset($value['price_type']) ? $value['price_type'] : null;
5855
$price = isset($value['price']) ? $value['price'] : null;
5956
$title = isset($value['title']) ? $value['title'] : null;

app/code/Magento/Catalog/Model/Product/Option/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function saveValues()
200200
'store_id',
201201
$this->getOption()->getStoreId()
202202
);
203-
203+
$this->unsetData('option_type_id');
204204
if ($this->getData('is_delete') == '1') {
205205
if ($this->getId()) {
206206
$this->deleteValues($this->getId());

0 commit comments

Comments
 (0)