Skip to content

Commit

Permalink
Merge pull request #979 from magento-south/BUGS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sergii Kovalenko authored Mar 31, 2017
2 parents 13ec629 + f04ce35 commit 50ed592
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 52 deletions.
25 changes: 0 additions & 25 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function execute()
$productTypeId = $this->getRequest()->getParam('type');
if ($data) {
try {
$this->unserializeProductData($data);
$product = $this->initializationHelper->initialize(
$this->productBuilder->build($this->getRequest())
);
Expand Down Expand Up @@ -181,30 +180,6 @@ public function execute()
return $resultRedirect;
}

/**
* Unserialize product data for configurable products
*
* @param array $postData
* @return void
*/
private function unserializeProductData($postData)
{
if (isset($postData["configurable-matrix-serialized"])) {
$configurableMatrixSerialized = $postData["configurable-matrix-serialized"];
if ($configurableMatrixSerialized != null && !empty($configurableMatrixSerialized)) {
$postData["configurable-matrix"] = json_decode($configurableMatrixSerialized, true);
unset($postData["configurable-matrix-serialized"]);
}
}
if (isset($postData["associated_product_ids_serialized"])) {
$associatedProductIdsSerialized = $postData["associated_product_ids_serialized"];
if ($associatedProductIdsSerialized != null && !empty($associatedProductIdsSerialized)) {
$postData["associated_product_ids"] = json_decode($associatedProductIdsSerialized, true);
unset($postData["associated_product_ids_serialized"]);
}
}
}

/**
* Notify customer when image was not deleted in specific case.
* TODO: temporary workaround must be eliminated in MAGETWO-45306
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,7 @@ define([
* Chose action for the form save button
*/
saveFormHandler: function () {
this.source.data['configurable-matrix-serialized'] =
JSON.stringify(this.source.data['configurable-matrix']);
delete this.source.data['configurable-matrix'];
this.source.data['associated_product_ids_serialized'] =
JSON.stringify(this.source.data['associated_product_ids']);
delete this.source.data['associated_product_ids'];
this.serializeData();

if (this.checkForNewAttributes()) {
this.formSaveParams = arguments;
Expand All @@ -398,6 +393,31 @@ define([
}
},

/**
* Serialize data for specific form fields
*
* Get data from outdated fields, serialize it and produce new form fields.
*
* Outdated fields:
* - configurable-matrix;
* - associated_product_ids.
*
* New fields:
* - configurable-matrix-serialized;
* - associated_product_ids_serialized.
*/
serializeData: function () {
this.source.data['configurable-matrix-serialized'] =
JSON.stringify(this.source.data['configurable-matrix']);

delete this.source.data['configurable-matrix'];

this.source.data['associated_product_ids_serialized'] =
JSON.stringify(this.source.data['associated_product_ids']);

delete this.source.data['associated_product_ids'];
},

/**
* Check for newly added attributes
* @returns {Boolean}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,36 @@
*/
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\ObjectManager;
use Magento\SalesRule\Model\CouponGenerator;

class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
{
/**
* @var CouponGenerator
*/
private $couponGenerator;

/**
* Generate constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
* @param CouponGenerator|null $couponGenerator
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
CouponGenerator $couponGenerator = null
) {
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
$this->couponGenerator = $couponGenerator ?:
$this->_objectManager->get(CouponGenerator::class);
}

/**
* Generate Coupons action
*
Expand All @@ -22,7 +50,6 @@ public function execute()
$result = [];
$this->_initRule();

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

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

/** @var $generator \Magento\SalesRule\Model\Coupon\Massgenerator */
$generator = $this->_objectManager->get(\Magento\SalesRule\Model\Coupon\Massgenerator::class);
if (!$generator->validateData($data)) {
$result['error'] = __('Invalid data provided');
} else {
$generator->setData($data);
$generator->generatePool();
$generated = $generator->getGeneratedCount();
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
$this->_view->getLayout()->initMessages();
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
}
$couponCodes = $this->couponGenerator->generateCodes($data);
$generated = count($couponCodes);
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
$this->_view->getLayout()->initMessages();
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
} catch (\Magento\Framework\Exception\InputException $inputException) {
$result['error'] = __('Invalid data provided');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$result['error'] = $e->getMessage();
} catch (\Exception $e) {
Expand Down
90 changes: 90 additions & 0 deletions app/code/Magento/SalesRule/Model/CouponGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesRule\Model;

/**
* Allows to generate a pool of coupon codes.
*
* Generated coupon code - auto generated string, which is used on checkout in order to get
* discount (fixed or in percents) on whole customer shopping cart or on items in this shopping cart.
* Class was added due to Backward Compatibility and is used as proxy to:
* @see \Magento\SalesRule\Model\Service\CouponManagementService
*/
class CouponGenerator
{
/**
* Map keys in old and new services
*
* Controller was used as old service
* @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
* - key = key in new service
* - value = key in old service
*
* @var array
*/
private $keyMap = [
'quantity' => 'qty'
];

/**
* @var Service\CouponManagementService
*/
private $couponManagementService;

/**
* @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory
*/
private $generationSpecFactory;

/**
* All objects should be injected through constructor, because we need to have working service already
* after it initializing
*
* @param Service\CouponManagementService $couponManagementService
* @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
*/
public function __construct(
\Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService,
\Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
) {
$this->couponManagementService = $couponManagementService;
$this->generationSpecFactory = $generationSpecFactory;
}

/**
* Generate a pool of generated coupon codes
*
* This method is used as proxy, due to high coupling in constructor
* @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
* In order to generate valid coupon codes, we need to initialize DTO object and run service.
* @see \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface -> DTO object
*
* @param array $parameters
* @return string[]
*/
public function generateCodes(array $parameters)
{
$couponSpecData = $this->convertCouponSpecData($parameters);
$couponSpec = $this->generationSpecFactory->create(['data' => $couponSpecData]);
return $this->couponManagementService->generate($couponSpec);
}

/**
* We should map old values to new one
* We need to do this, as new service with another key names was added
*
* @param array $data
* @return array
*/
private function convertCouponSpecData(array $data)
{
foreach ($this->keyMap as $mapKey => $mapValue) {
$data[$mapKey] = isset($data[$mapValue]) ? $data[$mapValue] : null;
}

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
namespace Magento\SalesRule\Model\Service;

use Magento\SalesRule\Model\Coupon;

/**
* Coupon management service class
*
Expand Down
Loading

0 comments on commit 50ed592

Please sign in to comment.