-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix conditions for gift rules: validate actions conditions, not valid…
…ate condition on gift item
- Loading branch information
1 parent
9210b3c
commit 3bce26a
Showing
10 changed files
with
378 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\GiftSalesRule | ||
* @author Pierre Le Maguer <[email protected]> | ||
* @copyright 2020 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\GiftSalesRule\Model; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Event\ManagerInterface; | ||
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator; | ||
use Magento\SalesRule\Model\Rule; | ||
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory; | ||
use Magento\SalesRule\Model\RulesApplier as BaseRulesApplier; | ||
use Magento\SalesRule\Model\Utility; | ||
|
||
/** | ||
* Model Rules Applier | ||
* | ||
* @author Pierre Le Maguer <[email protected]> | ||
* @copyright 2020 Smile | ||
*/ | ||
class RulesApplier extends BaseRulesApplier | ||
{ | ||
/** | ||
* Is a private parent property. | ||
* @var ChildrenValidationLocator | ||
*/ | ||
protected $childrenValidationLocator; | ||
|
||
/** | ||
* Is a private parent property. | ||
* @var CalculatorFactory | ||
*/ | ||
protected $calculatorFactory; | ||
|
||
/** | ||
* RulesApplier constructor. | ||
* | ||
* @param CalculatorFactory $calculatorFactory calculator factory | ||
* @param ManagerInterface $eventManager event manager | ||
* @param Utility $utility utility | ||
* @param ChildrenValidationLocator|null $childrenValidationLocator children validation locator | ||
*/ | ||
public function __construct( | ||
CalculatorFactory $calculatorFactory, | ||
ManagerInterface $eventManager, | ||
Utility $utility, | ||
ChildrenValidationLocator $childrenValidationLocator | ||
) { | ||
parent::__construct($calculatorFactory, $eventManager, $utility, $childrenValidationLocator); | ||
$this->calculatorFactory = $calculatorFactory; | ||
$this->childrenValidationLocator = $childrenValidationLocator; | ||
} | ||
|
||
/** | ||
* Edit: Add an event skip actions validation for gift rule. | ||
* | ||
* {@inheritdoc} | ||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
*/ | ||
public function applyRules($item, $rules, $skipValidation, $couponCode) | ||
{ | ||
$address = $item->getAddress(); | ||
$appliedRuleIds = []; | ||
/* @var $rule Rule */ | ||
foreach ($rules as $rule) { | ||
if (!$this->validatorUtility->canProcessRule($rule, $address)) { | ||
continue; | ||
} | ||
|
||
// Edit Smile: Dispatch an event to be able to edit the actions skip validation. | ||
$dataObject = new DataObject(['skip_validation' => $skipValidation]); | ||
$this->_eventManager->dispatch( | ||
'sales_rules_skip_actions_validation', | ||
['skip_validation_object' => $dataObject, 'rule' => $rule] | ||
); | ||
$skipValidationForGift = $dataObject->getData('skip_validation'); | ||
if (!$skipValidation && !$rule->getActions()->validate($item) && !$skipValidationForGift) { | ||
// Edit Smile End. | ||
if (!$this->childrenValidationLocator->isChildrenValidationRequired($item)) { | ||
continue; | ||
} | ||
$childItems = $item->getChildren(); | ||
$isContinue = true; | ||
if (!empty($childItems)) { | ||
foreach ($childItems as $childItem) { | ||
if ($rule->getActions()->validate($childItem)) { | ||
$isContinue = false; | ||
} | ||
} | ||
} | ||
if ($isContinue) { | ||
continue; | ||
} | ||
} | ||
|
||
$this->applyRule($item, $rule, $address, $couponCode); | ||
$appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId(); | ||
|
||
if ($rule->getStopRulesProcessing()) { | ||
break; | ||
} | ||
} | ||
|
||
return $appliedRuleIds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\GiftSalesRule | ||
* @author Pierre Le Maguer <[email protected]> | ||
* @copyright 2020 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\GiftSalesRule\Observer; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\SalesRule\Model\Rule; | ||
use Smile\GiftSalesRule\Helper\GiftRule as GiftRuleHelper; | ||
|
||
/** | ||
* Class ValidateGiftRuleActions | ||
* | ||
* @author Pierre Le Maguer <[email protected]> | ||
* @copyright 2020 Smile | ||
*/ | ||
class ValidateGiftRuleActions implements ObserverInterface | ||
{ | ||
/** | ||
* @var GiftRuleHelper | ||
*/ | ||
protected $giftRuleHelper; | ||
|
||
/** | ||
* ValidateGiftRuleActions constructor. | ||
* | ||
* @param GiftRuleHelper $giftRuleHelper Gift rule helper | ||
*/ | ||
public function __construct(GiftRuleHelper $giftRuleHelper) | ||
{ | ||
$this->giftRuleHelper = $giftRuleHelper; | ||
} | ||
|
||
/** | ||
* Skip actions validation if the rule is a gift sales rule. | ||
* | ||
* @param Observer $observer Observer | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var Rule $rule */ | ||
$rule = $observer->getEvent()->getData('rule'); | ||
/** @var DataObject $skipValidationObject */ | ||
$skipValidationObject = $observer->getEvent()->getData('skip_validation_object'); | ||
$skipValidation = $skipValidationObject->getData('skip_validation'); | ||
|
||
if (!$skipValidation) { | ||
$skipValidation = $this->giftRuleHelper->isGiftRule($rule); | ||
} | ||
|
||
$skipValidationObject->setData('skip_validation', $skipValidation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,21 @@ | |
|
||
use Magento\Checkout\Block\Cart\Item\Renderer; | ||
use Magento\Quote\Model\Quote\Item\AbstractItem; | ||
use Smile\GiftSalesRule\Helper\GiftRule; | ||
|
||
/** | ||
* Class CombinePlugin | ||
* Class RendererPlugin | ||
* | ||
* @author Pierre Le Maguer <[email protected]> | ||
* @copyright 2019 Smile | ||
*/ | ||
class RendererPlugin | ||
{ | ||
/** | ||
* @var GiftRule | ||
*/ | ||
protected $giftRuleHelper; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -33,6 +39,14 @@ class RendererPlugin | |
'checkout.cart.item.renderers.configurable.actions.edit', | ||
]; | ||
|
||
/** | ||
* @param GiftRule $giftRuleHelper gift rule helper | ||
*/ | ||
public function __construct(GiftRule $giftRuleHelper) | ||
{ | ||
$this->giftRuleHelper = $giftRuleHelper; | ||
} | ||
|
||
/** | ||
* Remove the edit action from the item renderer for gift items. | ||
* | ||
|
@@ -44,9 +58,8 @@ class RendererPlugin | |
public function beforeGetActions( | ||
Renderer $subject, | ||
AbstractItem $item | ||
) { | ||
$option = $item->getOptionByCode('option_gift_rule'); | ||
if ($option) { | ||
): array { | ||
if ($this->giftRuleHelper->isGiftItem($item)) { | ||
$actionsBlock = $subject->getChildBlock('actions'); | ||
if ($actionsBlock) { | ||
foreach ($this->actionsBlockToRemove as $blockName) { | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.