Skip to content

Commit 8f38ed2

Browse files
authored
Merge pull request #3140 from magento-tango/PR-2008-2.3-tmp
[tango] PR 2.3
2 parents ac3ae89 + ca643fe commit 8f38ed2

File tree

35 files changed

+1840
-816
lines changed

35 files changed

+1840
-816
lines changed

app/code/Magento/Backend/view/adminhtml/web/js/media-uploader.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,10 @@ define([
123123
this.element.find('input[type=file]').fileupload('option', {
124124
process: [{
125125
action: 'load',
126-
fileTypes: /^image\/(gif|jpeg|png)$/
126+
fileTypes: /^image\/(gif|jpeg|png)$/,
127+
maxFileSize: this.options.maxFileSize
127128
}, {
128-
action: 'resize',
129-
maxWidth: this.options.maxWidth,
130-
maxHeight: this.options.maxHeight
129+
action: 'resize'
131130
}, {
132131
action: 'save'
133132
}]

app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php

Lines changed: 11 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ public function validate($object)
247247
}
248248

249249
/**
250+
* Validate price.
251+
*
250252
* @param array $priceRow
251253
* @return void
252254
* @throws \Magento\Framework\Exception\LocalizedException
@@ -312,6 +314,8 @@ public function afterLoad($object)
312314
}
313315

314316
/**
317+
* Get website id.
318+
*
315319
* @param int $storeId
316320
* @return int|null
317321
*/
@@ -327,6 +331,8 @@ private function getWebsiteId($storeId)
327331
}
328332

329333
/**
334+
* Set price data.
335+
*
330336
* @param \Magento\Catalog\Model\Product $object
331337
* @param array $priceData
332338
*/
@@ -373,122 +379,16 @@ protected function modifyPriceData($object, $data)
373379
*
374380
* @param \Magento\Catalog\Model\Product $object
375381
* @return $this
376-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
377-
* @SuppressWarnings(PHPMD.NPathComplexity)
378-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
382+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
379383
*/
380384
public function afterSave($object)
381385
{
382-
$websiteId = $this->_storeManager->getStore($object->getStoreId())->getWebsiteId();
383-
$isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;
384-
385-
$priceRows = $object->getData($this->getAttribute()->getName());
386-
if (null === $priceRows) {
387-
return $this;
388-
}
389-
390-
$priceRows = array_filter((array)$priceRows);
391-
392-
$old = [];
393-
$new = [];
394-
395-
// prepare original data for compare
396-
$origPrices = $object->getOrigData($this->getAttribute()->getName());
397-
if (!is_array($origPrices)) {
398-
$origPrices = [];
399-
}
400-
foreach ($origPrices as $data) {
401-
if ($data['website_id'] > 0 || $data['website_id'] == '0' && $isGlobal) {
402-
$key = implode(
403-
'-',
404-
array_merge(
405-
[$data['website_id'], $data['cust_group']],
406-
$this->_getAdditionalUniqueFields($data)
407-
)
408-
);
409-
$old[$key] = $data;
410-
}
411-
}
412-
413-
// prepare data for save
414-
foreach ($priceRows as $data) {
415-
$hasEmptyData = false;
416-
foreach ($this->_getAdditionalUniqueFields($data) as $field) {
417-
if (empty($field)) {
418-
$hasEmptyData = true;
419-
break;
420-
}
421-
}
422-
423-
if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
424-
continue;
425-
}
426-
if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
427-
continue;
428-
}
429-
if (!$isGlobal && (int)$data['website_id'] == 0) {
430-
continue;
431-
}
432-
433-
$key = implode(
434-
'-',
435-
array_merge([$data['website_id'], $data['cust_group']], $this->_getAdditionalUniqueFields($data))
436-
);
437-
438-
$useForAllGroups = $data['cust_group'] == $this->_groupManagement->getAllCustomersGroup()->getId();
439-
$customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
440-
$new[$key] = array_merge(
441-
$this->getAdditionalFields($data),
442-
[
443-
'website_id' => $data['website_id'],
444-
'all_groups' => $useForAllGroups ? 1 : 0,
445-
'customer_group_id' => $customerGroupId,
446-
'value' => isset($data['price']) ? $data['price'] : null,
447-
],
448-
$this->_getAdditionalUniqueFields($data)
449-
);
450-
}
451-
452-
$delete = array_diff_key($old, $new);
453-
$insert = array_diff_key($new, $old);
454-
$update = array_intersect_key($new, $old);
455-
456-
$isChanged = false;
457-
$productId = $object->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField());
458-
459-
if (!empty($delete)) {
460-
foreach ($delete as $data) {
461-
$this->_getResource()->deletePriceData($productId, null, $data['price_id']);
462-
$isChanged = true;
463-
}
464-
}
465-
466-
if (!empty($insert)) {
467-
foreach ($insert as $data) {
468-
$price = new \Magento\Framework\DataObject($data);
469-
$price->setData(
470-
$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(),
471-
$productId
472-
);
473-
$this->_getResource()->savePriceData($price);
474-
475-
$isChanged = true;
476-
}
477-
}
478-
479-
if (!empty($update)) {
480-
$isChanged |= $this->updateValues($update, $old);
481-
}
482-
483-
if ($isChanged) {
484-
$valueChangedKey = $this->getAttribute()->getName() . '_changed';
485-
$object->setData($valueChangedKey, 1);
486-
}
487-
488386
return $this;
489387
}
490388

491389
/**
390+
* Update values.
391+
*
492392
* @param array $valuesToUpdate
493393
* @param array $oldValues
494394
* @return boolean
@@ -544,6 +444,8 @@ public function getResource()
544444
}
545445

546446
/**
447+
* Get metadata pool.
448+
*
547449
* @return \Magento\Framework\EntityManager\MetadataPool
548450
*/
549451
private function getMetadataPool()
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Attribute\Backend\TierPrice;
9+
10+
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Catalog\Api\Data\ProductInterface;
14+
use Magento\Customer\Api\GroupManagementInterface;
15+
use Magento\Framework\EntityManager\MetadataPool;
16+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice;
17+
18+
/**
19+
* Process tier price data for handled new product
20+
*/
21+
class SaveHandler implements ExtensionInterface
22+
{
23+
/**
24+
* @var \Magento\Store\Model\StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
30+
*/
31+
private $attributeRepository;
32+
33+
/**
34+
* @var \Magento\Customer\Api\GroupManagementInterface
35+
*/
36+
private $groupManagement;
37+
38+
/**
39+
* @var \Magento\Framework\EntityManager\MetadataPool
40+
*/
41+
private $metadataPoll;
42+
43+
/**
44+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice
45+
*/
46+
private $tierPriceResource;
47+
48+
/**
49+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
50+
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
51+
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
52+
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
53+
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice $tierPriceResource
54+
*/
55+
public function __construct(
56+
StoreManagerInterface $storeManager,
57+
ProductAttributeRepositoryInterface $attributeRepository,
58+
GroupManagementInterface $groupManagement,
59+
MetadataPool $metadataPool,
60+
Tierprice $tierPriceResource
61+
) {
62+
$this->storeManager = $storeManager;
63+
$this->attributeRepository = $attributeRepository;
64+
$this->groupManagement = $groupManagement;
65+
$this->metadataPoll = $metadataPool;
66+
$this->tierPriceResource = $tierPriceResource;
67+
}
68+
69+
/**
70+
* Set tier price data for product entity
71+
*
72+
* @param \Magento\Catalog\Api\Data\ProductInterface|object $entity
73+
* @param array $arguments
74+
* @return \Magento\Catalog\Api\Data\ProductInterface|object
75+
* @throws \Magento\Framework\Exception\NoSuchEntityException
76+
* @throws \Magento\Framework\Exception\LocalizedException
77+
* @throws \Magento\Framework\Exception\InputException
78+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
79+
*/
80+
public function execute($entity, $arguments = [])
81+
{
82+
$attribute = $this->attributeRepository->get('tier_price');
83+
$priceRows = $entity->getData($attribute->getName());
84+
if (null !== $priceRows) {
85+
if (!is_array($priceRows)) {
86+
throw new \Magento\Framework\Exception\InputException(
87+
__('Tier prices data should be array, but actually other type is received')
88+
);
89+
}
90+
$websiteId = $this->storeManager->getStore($entity->getStoreId())->getWebsiteId();
91+
$isGlobal = $attribute->isScopeGlobal() || $websiteId === 0;
92+
$identifierField = $this->metadataPoll->getMetadata(ProductInterface::class)->getLinkField();
93+
$priceRows = array_filter($priceRows);
94+
$productId = (int) $entity->getData($identifierField);
95+
96+
// prepare and save data
97+
foreach ($priceRows as $data) {
98+
$isPriceWebsiteGlobal = (int)$data['website_id'] === 0;
99+
if ($isGlobal === $isPriceWebsiteGlobal
100+
|| !empty($data['price_qty'])
101+
|| isset($data['cust_group'])
102+
) {
103+
$tierPrice = $this->prepareTierPrice($data);
104+
$price = new \Magento\Framework\DataObject($tierPrice);
105+
$price->setData(
106+
$identifierField,
107+
$productId
108+
);
109+
$this->tierPriceResource->savePriceData($price);
110+
$valueChangedKey = $attribute->getName() . '_changed';
111+
$entity->setData($valueChangedKey, 1);
112+
}
113+
}
114+
}
115+
116+
return $entity;
117+
}
118+
119+
/**
120+
* Get additional tier price fields
121+
*
122+
* @param array $objectArray
123+
* @return array
124+
*/
125+
private function getAdditionalFields(array $objectArray): array
126+
{
127+
$percentageValue = $this->getPercentage($objectArray);
128+
return [
129+
'value' => $percentageValue ? null : $objectArray['price'],
130+
'percentage_value' => $percentageValue ?: null,
131+
];
132+
}
133+
134+
/**
135+
* Check whether price has percentage value.
136+
*
137+
* @param array $priceRow
138+
* @return int|null
139+
*/
140+
private function getPercentage(array $priceRow): ?int
141+
{
142+
return isset($priceRow['percentage_value']) && is_numeric($priceRow['percentage_value'])
143+
? (int)$priceRow['percentage_value']
144+
: null;
145+
}
146+
147+
/**
148+
* Prepare tier price data by provided price row data
149+
*
150+
* @param array $data
151+
* @return array
152+
* @throws \Magento\Framework\Exception\LocalizedException
153+
*/
154+
private function prepareTierPrice(array $data): array
155+
{
156+
$useForAllGroups = (int)$data['cust_group'] === $this->groupManagement->getAllCustomersGroup()->getId();
157+
$customerGroupId = $useForAllGroups ? 0 : $data['cust_group'];
158+
$tierPrice = array_merge(
159+
$this->getAdditionalFields($data),
160+
[
161+
'website_id' => $data['website_id'],
162+
'all_groups' => (int)$useForAllGroups,
163+
'customer_group_id' => $customerGroupId,
164+
'value' => $data['price'] ?? null,
165+
'qty' => (int)$data['price_qty']
166+
]
167+
);
168+
169+
return $tierPrice;
170+
}
171+
}

0 commit comments

Comments
 (0)