Skip to content

Commit

Permalink
Merge pull request #33 from cfman/fix-free-gift-cannot-be-added-to-vi…
Browse files Browse the repository at this point in the history
…rtual-quote

Fix the case where free gift cannot be added to a virtual quote
  • Loading branch information
domeglic authored Dec 9, 2020
2 parents a328550 + 29b260c commit ea15d93
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 ea15d93

Please sign in to comment.