Skip to content

Commit

Permalink
Fix conditions for gift rules: validate actions conditions, not valid…
Browse files Browse the repository at this point in the history
…ate condition on gift item
  • Loading branch information
PierreLeMaguer authored and maximequeneau committed Oct 22, 2020
1 parent 9210b3c commit 3bce26a
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 77 deletions.
14 changes: 13 additions & 1 deletion Helper/GiftRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\SalesRule\Model\Rule;
use Smile\GiftSalesRule\Api\Data\GiftRuleInterface;
use Smile\GiftSalesRule\Api\GiftRuleRepositoryInterface;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function isValidGiftRule(Rule $rule, Quote $quote)
*/
$hasProduct = false;
foreach ($quote->getAllItems() as $item) {
if (!$item->getOptionByCode('option_gift_rule')) {
if (!$this->isGiftItem($item)) {
$hasProduct = true;
break;
}
Expand Down Expand Up @@ -147,4 +148,15 @@ public function getRange($quote, $giftRule)
{
return floor($quote->getGrandTotal() / $giftRule->getPriceRange());
}

/**
* Is gift item ?
*
* @param AbstractItem $item item
* @return bool
*/
public function isGiftItem(AbstractItem $item): bool
{
return (bool) $item->getOptionByCode('option_gift_rule');
}
}
115 changes: 115 additions & 0 deletions Model/RulesApplier.php
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;
}
}
16 changes: 15 additions & 1 deletion Observer/SetGiftItemPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Smile\GiftSalesRule\Helper\GiftRule;

/**
* Class SetGiftItemPrice
Expand All @@ -24,6 +25,19 @@
*/
class SetGiftItemPrice implements ObserverInterface
{
/**
* @var GiftRule
*/
protected $giftRuleHelper;

/**
* @param GiftRule $giftRuleHelper gift rule helper
*/
public function __construct(GiftRule $giftRuleHelper)
{
$this->giftRuleHelper = $giftRuleHelper;
}

/**
* Change price for gift product
*
Expand All @@ -33,7 +47,7 @@ public function execute(Observer $observer)
{
/** @var \Magento\Quote\Model\Quote\Item $item */
$item = $observer->getEvent()->getData('quote_item');
if ($item->getOptionByCode('option_gift_rule') !== null) {
if ($this->giftRuleHelper->isGiftItem($item)) {
$item->setCustomPrice(0);
$item->setOriginalCustomPrice(0);
}
Expand Down
64 changes: 64 additions & 0 deletions Observer/ValidateGiftRuleActions.php
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);
}
}
21 changes: 17 additions & 4 deletions Plugin/Checkout/Block/Cart/Item/RendererPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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.
*
Expand All @@ -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) {
Expand Down
69 changes: 0 additions & 69 deletions Plugin/Model/Rule/Condition/Product/CombinePlugin.php

This file was deleted.

Loading

0 comments on commit 3bce26a

Please sign in to comment.