Skip to content

Commit 50ed592

Browse files
author
Sergii Kovalenko
authored
Merge pull request #979 from magento-south/BUGS
Fixed issues: - MAGETWO-58651 Scheduled Update removes all simple variations from Configurable product - MAGETWO-60756 "Uses per Coupon" limit does not work for auto generated coupons - MAGETWO-61425 Fix timeline status detection - MAGETWO-59343 Automate new update wishlist flow - MAGETWO-65696 Automate VAT ID domestic group validation test
2 parents 13ec629 + f04ce35 commit 50ed592

File tree

13 files changed

+433
-52
lines changed

13 files changed

+433
-52
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function execute()
9797
$productTypeId = $this->getRequest()->getParam('type');
9898
if ($data) {
9999
try {
100-
$this->unserializeProductData($data);
101100
$product = $this->initializationHelper->initialize(
102101
$this->productBuilder->build($this->getRequest())
103102
);
@@ -181,30 +180,6 @@ public function execute()
181180
return $resultRedirect;
182181
}
183182

184-
/**
185-
* Unserialize product data for configurable products
186-
*
187-
* @param array $postData
188-
* @return void
189-
*/
190-
private function unserializeProductData($postData)
191-
{
192-
if (isset($postData["configurable-matrix-serialized"])) {
193-
$configurableMatrixSerialized = $postData["configurable-matrix-serialized"];
194-
if ($configurableMatrixSerialized != null && !empty($configurableMatrixSerialized)) {
195-
$postData["configurable-matrix"] = json_decode($configurableMatrixSerialized, true);
196-
unset($postData["configurable-matrix-serialized"]);
197-
}
198-
}
199-
if (isset($postData["associated_product_ids_serialized"])) {
200-
$associatedProductIdsSerialized = $postData["associated_product_ids_serialized"];
201-
if ($associatedProductIdsSerialized != null && !empty($associatedProductIdsSerialized)) {
202-
$postData["associated_product_ids"] = json_decode($associatedProductIdsSerialized, true);
203-
unset($postData["associated_product_ids_serialized"]);
204-
}
205-
}
206-
}
207-
208183
/**
209184
* Notify customer when image was not deleted in specific case.
210185
* TODO: temporary workaround must be eliminated in MAGETWO-45306

app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/variations.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,7 @@ define([
383383
* Chose action for the form save button
384384
*/
385385
saveFormHandler: function () {
386-
this.source.data['configurable-matrix-serialized'] =
387-
JSON.stringify(this.source.data['configurable-matrix']);
388-
delete this.source.data['configurable-matrix'];
389-
this.source.data['associated_product_ids_serialized'] =
390-
JSON.stringify(this.source.data['associated_product_ids']);
391-
delete this.source.data['associated_product_ids'];
386+
this.serializeData();
392387

393388
if (this.checkForNewAttributes()) {
394389
this.formSaveParams = arguments;
@@ -398,6 +393,31 @@ define([
398393
}
399394
},
400395

396+
/**
397+
* Serialize data for specific form fields
398+
*
399+
* Get data from outdated fields, serialize it and produce new form fields.
400+
*
401+
* Outdated fields:
402+
* - configurable-matrix;
403+
* - associated_product_ids.
404+
*
405+
* New fields:
406+
* - configurable-matrix-serialized;
407+
* - associated_product_ids_serialized.
408+
*/
409+
serializeData: function () {
410+
this.source.data['configurable-matrix-serialized'] =
411+
JSON.stringify(this.source.data['configurable-matrix']);
412+
413+
delete this.source.data['configurable-matrix'];
414+
415+
this.source.data['associated_product_ids_serialized'] =
416+
JSON.stringify(this.source.data['associated_product_ids']);
417+
418+
delete this.source.data['associated_product_ids'];
419+
},
420+
401421
/**
402422
* Check for newly added attributes
403423
* @returns {Boolean}

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Generate.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,36 @@
66
*/
77
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\SalesRule\Model\CouponGenerator;
11+
912
class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
1013
{
14+
/**
15+
* @var CouponGenerator
16+
*/
17+
private $couponGenerator;
18+
19+
/**
20+
* Generate constructor.
21+
* @param \Magento\Backend\App\Action\Context $context
22+
* @param \Magento\Framework\Registry $coreRegistry
23+
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
24+
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
25+
* @param CouponGenerator|null $couponGenerator
26+
*/
27+
public function __construct(
28+
\Magento\Backend\App\Action\Context $context,
29+
\Magento\Framework\Registry $coreRegistry,
30+
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
31+
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
32+
CouponGenerator $couponGenerator = null
33+
) {
34+
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
35+
$this->couponGenerator = $couponGenerator ?:
36+
$this->_objectManager->get(CouponGenerator::class);
37+
}
38+
1139
/**
1240
* Generate Coupons action
1341
*
@@ -22,7 +50,6 @@ public function execute()
2250
$result = [];
2351
$this->_initRule();
2452

25-
/** @var $rule \Magento\SalesRule\Model\Rule */
2653
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
2754

2855
if (!$rule->getId()) {
@@ -35,18 +62,13 @@ public function execute()
3562
$data = $inputFilter->getUnescaped();
3663
}
3764

38-
/** @var $generator \Magento\SalesRule\Model\Coupon\Massgenerator */
39-
$generator = $this->_objectManager->get(\Magento\SalesRule\Model\Coupon\Massgenerator::class);
40-
if (!$generator->validateData($data)) {
41-
$result['error'] = __('Invalid data provided');
42-
} else {
43-
$generator->setData($data);
44-
$generator->generatePool();
45-
$generated = $generator->getGeneratedCount();
46-
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
47-
$this->_view->getLayout()->initMessages();
48-
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
49-
}
65+
$couponCodes = $this->couponGenerator->generateCodes($data);
66+
$generated = count($couponCodes);
67+
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
68+
$this->_view->getLayout()->initMessages();
69+
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
70+
} catch (\Magento\Framework\Exception\InputException $inputException) {
71+
$result['error'] = __('Invalid data provided');
5072
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5173
$result['error'] = $e->getMessage();
5274
} catch (\Exception $e) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Model;
7+
8+
/**
9+
* Allows to generate a pool of coupon codes.
10+
*
11+
* Generated coupon code - auto generated string, which is used on checkout in order to get
12+
* discount (fixed or in percents) on whole customer shopping cart or on items in this shopping cart.
13+
* Class was added due to Backward Compatibility and is used as proxy to:
14+
* @see \Magento\SalesRule\Model\Service\CouponManagementService
15+
*/
16+
class CouponGenerator
17+
{
18+
/**
19+
* Map keys in old and new services
20+
*
21+
* Controller was used as old service
22+
* @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
23+
* - key = key in new service
24+
* - value = key in old service
25+
*
26+
* @var array
27+
*/
28+
private $keyMap = [
29+
'quantity' => 'qty'
30+
];
31+
32+
/**
33+
* @var Service\CouponManagementService
34+
*/
35+
private $couponManagementService;
36+
37+
/**
38+
* @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory
39+
*/
40+
private $generationSpecFactory;
41+
42+
/**
43+
* All objects should be injected through constructor, because we need to have working service already
44+
* after it initializing
45+
*
46+
* @param Service\CouponManagementService $couponManagementService
47+
* @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
48+
*/
49+
public function __construct(
50+
\Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService,
51+
\Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
52+
) {
53+
$this->couponManagementService = $couponManagementService;
54+
$this->generationSpecFactory = $generationSpecFactory;
55+
}
56+
57+
/**
58+
* Generate a pool of generated coupon codes
59+
*
60+
* This method is used as proxy, due to high coupling in constructor
61+
* @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
62+
* In order to generate valid coupon codes, we need to initialize DTO object and run service.
63+
* @see \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface -> DTO object
64+
*
65+
* @param array $parameters
66+
* @return string[]
67+
*/
68+
public function generateCodes(array $parameters)
69+
{
70+
$couponSpecData = $this->convertCouponSpecData($parameters);
71+
$couponSpec = $this->generationSpecFactory->create(['data' => $couponSpecData]);
72+
return $this->couponManagementService->generate($couponSpec);
73+
}
74+
75+
/**
76+
* We should map old values to new one
77+
* We need to do this, as new service with another key names was added
78+
*
79+
* @param array $data
80+
* @return array
81+
*/
82+
private function convertCouponSpecData(array $data)
83+
{
84+
foreach ($this->keyMap as $mapKey => $mapValue) {
85+
$data[$mapKey] = isset($data[$mapValue]) ? $data[$mapValue] : null;
86+
}
87+
88+
return $data;
89+
}
90+
}

app/code/Magento/SalesRule/Model/Service/CouponManagementService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\SalesRule\Model\Service;
77

8-
use Magento\SalesRule\Model\Coupon;
9-
108
/**
119
* Coupon management service class
1210
*

0 commit comments

Comments
 (0)