Skip to content

Commit

Permalink
Fix the case where free gift cannot be added to a virtual quote
Browse files Browse the repository at this point in the history
  • Loading branch information
John Chan committed Dec 9, 2020
1 parent a328550 commit 29b260c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Observer/ResetGiftItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,18 @@ public function execute(Observer $observer)
/** @var Quote\Address $address */
$address = $shippingAssignment->getShipping()->getAddress();

if ($address->getAddressType() != Quote\Address::ADDRESS_TYPE_SHIPPING)
if (($quote->isVirtual() && $address->getAddressType() !== Quote\Address::ADDRESS_TYPE_BILLING)
|| (!$quote->isVirtual() && $address->getAddressType() !== Quote\Address::ADDRESS_TYPE_SHIPPING))
{
return;
}
} else
{
$address = $quote->getShippingAddress();
if ($quote->isVirtual()) {
$address = $quote->getBillingAddress();
} else {
$address = $quote->getShippingAddress();
}
}

$realQuoteItems = $this->removeOldGiftQuoteItems($quote);
Expand Down
15 changes: 14 additions & 1 deletion SalesRule/Action/AbstractGiftAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,24 @@ protected function canApplyRule(Quote\Item $item, Rule $rule, DataObject $stateO
$appliedRuleIds = $stateObject->getData(static::APPLIED_FREEPRODUCT_RULE_IDS) ?? [];

return (
$item->getAddress()->getAddressType() == Address::TYPE_SHIPPING &&
$this->isValidAddressType($item) &&
isset($appliedRuleIds[$rule->getId()]) == false
);
}

/**
* @param Quote\Item|AbstractItem $item
* @return bool
*/
protected function isValidAddressType($item)
{
if ($item->getQuote()->isVirtual()) {
return $item->getAddress()->getAddressType() === Address::TYPE_BILLING;
} else {
return $item->getAddress()->getAddressType() === Address::TYPE_SHIPPING;
}
}

/**
* Qty of gift item that will be added
*
Expand Down

0 comments on commit 29b260c

Please sign in to comment.