Skip to content

Commit

Permalink
Merge pull request #102 from IrinaZhadzinets/issue-4802
Browse files Browse the repository at this point in the history
#4802 - Return only free payment if available in SaveAddress function
  • Loading branch information
carinadues authored Aug 1, 2022
2 parents eb724b5 + bef1bee commit 3a368ed
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions src/Model/Resolver/SaveAddressInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class SaveAddressInformation implements ResolverInterface {
*/
protected const PICKUP_METHOD_CODE = 'pickup';

/**
* Zero Subtotal Checkout payment method
*/
protected const FREE_PAYMENT = 'free';

/**
* SaveAddressInformation constructor.
* @param ShippingInformationManagementInterface $shippingInformationManagement
Expand Down Expand Up @@ -170,6 +175,52 @@ protected function requestPaymentMethods($addressInformation, $guestCartId = nul
// The following crutch exists because of Magento not being able to fix an issue for four years
// Link to the issue: https://github.com/magento/magento2/issues/7769
$rawTotals->setGrandTotal($rawTotals->getTotalSegments()['grand_total']['value']);
$totals = array_merge(
$rawTotals->getData(),
[ 'items' => array_map(function ($item) {
/** @var TotalsItemInterface $item */
return [
TotalsItemInterface::KEY_ITEM_ID => $item->getItemId(),
TotalsItemInterface::KEY_PRICE => $item->getPrice(),
TotalsItemInterface::KEY_BASE_PRICE => $item->getBasePrice(),
TotalsItemInterface::KEY_QTY => $item->getQty(),
TotalsItemInterface::KEY_ROW_TOTAL => $item->getRowTotal(),
TotalsItemInterface::KEY_BASE_ROW_TOTAL => $item->getBaseRowTotal(),
TotalsItemInterface::KEY_ROW_TOTAL_WITH_DISCOUNT => $item->getRowTotalWithDiscount(),
TotalsItemInterface::KEY_TAX_AMOUNT => $item->getTaxAmount(),
TotalsItemInterface::KEY_BASE_TAX_AMOUNT => $item->getBaseTaxAmount(),
TotalsItemInterface::KEY_TAX_PERCENT => $item->getTaxPercent(),
TotalsItemInterface::KEY_DISCOUNT_AMOUNT => $item->getDiscountAmount(),
TotalsItemInterface::KEY_BASE_DISCOUNT_AMOUNT => $item->getBaseDiscountAmount(),
TotalsItemInterface::KEY_DISCOUNT_PERCENT => $item->getDiscountPercent(),
TotalsItemInterface::KEY_PRICE_INCL_TAX => $item->getPriceInclTax(),
TotalsItemInterface::KEY_BASE_PRICE_INCL_TAX => $item->getBasePriceInclTax(),
TotalsItemInterface::KEY_ROW_TOTAL_INCL_TAX => $item->getRowTotalInclTax(),
TotalsItemInterface::KEY_BASE_ROW_TOTAL_INCL_TAX => $item->getBaseRowTotalInclTax(),
TotalsItemInterface::KEY_OPTIONS => $item->getOptions(),
TotalsItemInterface::KEY_WEEE_TAX_APPLIED_AMOUNT => $item->getWeeeTaxAppliedAmount(),
TotalsItemInterface::KEY_WEEE_TAX_APPLIED => $item->getWeeeTaxApplied(),
TotalsItemInterface::KEY_NAME => $item->getName(),
];
}, $rawTotals->getItems()) ]
);

$paymentMethods = $rawPaymentInformation->getPaymentMethods();

/** If Zero Subtotal Checkout is available filter out all the other methods and return only Free Payment */
foreach ($paymentMethods as $payment) {
if ($payment->getCode() === self::FREE_PAYMENT) {
return [
'payment_methods' => [
[
'code' => $payment->getCode(),
'title' => $payment->getTitle()
]
],
'totals' => $totals
];
}
}

return [
'payment_methods' => array_map(
Expand All @@ -182,35 +233,7 @@ function ($payment) {
},
$rawPaymentInformation->getPaymentMethods()
),
'totals' => array_merge(
$rawTotals->getData(),
[ 'items' => array_map(function ($item) {
/** @var TotalsItemInterface $item */
return [
TotalsItemInterface::KEY_ITEM_ID => $item->getItemId(),
TotalsItemInterface::KEY_PRICE => $item->getPrice(),
TotalsItemInterface::KEY_BASE_PRICE => $item->getBasePrice(),
TotalsItemInterface::KEY_QTY => $item->getQty(),
TotalsItemInterface::KEY_ROW_TOTAL => $item->getRowTotal(),
TotalsItemInterface::KEY_BASE_ROW_TOTAL => $item->getBaseRowTotal(),
TotalsItemInterface::KEY_ROW_TOTAL_WITH_DISCOUNT => $item->getRowTotalWithDiscount(),
TotalsItemInterface::KEY_TAX_AMOUNT => $item->getTaxAmount(),
TotalsItemInterface::KEY_BASE_TAX_AMOUNT => $item->getBaseTaxAmount(),
TotalsItemInterface::KEY_TAX_PERCENT => $item->getTaxPercent(),
TotalsItemInterface::KEY_DISCOUNT_AMOUNT => $item->getDiscountAmount(),
TotalsItemInterface::KEY_BASE_DISCOUNT_AMOUNT => $item->getBaseDiscountAmount(),
TotalsItemInterface::KEY_DISCOUNT_PERCENT => $item->getDiscountPercent(),
TotalsItemInterface::KEY_PRICE_INCL_TAX => $item->getPriceInclTax(),
TotalsItemInterface::KEY_BASE_PRICE_INCL_TAX => $item->getBasePriceInclTax(),
TotalsItemInterface::KEY_ROW_TOTAL_INCL_TAX => $item->getRowTotalInclTax(),
TotalsItemInterface::KEY_BASE_ROW_TOTAL_INCL_TAX => $item->getBaseRowTotalInclTax(),
TotalsItemInterface::KEY_OPTIONS => $item->getOptions(),
TotalsItemInterface::KEY_WEEE_TAX_APPLIED_AMOUNT => $item->getWeeeTaxAppliedAmount(),
TotalsItemInterface::KEY_WEEE_TAX_APPLIED => $item->getWeeeTaxApplied(),
TotalsItemInterface::KEY_NAME => $item->getName(),
];
}, $rawTotals->getItems()) ]
)
'totals' => $totals
];
}
}

0 comments on commit 3a368ed

Please sign in to comment.