Last active
July 17, 2020 09:00
-
-
Save snez/58a78ad19a558b5b8152e22fb6c8ffa8 to your computer and use it in GitHub Desktop.
Magento 2 break down quote into component parts
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
<?php | |
class SomeClass | |
{ | |
public function __construct( | |
\StripeIntegration\Payments\Helper\Generic $helper, | |
\Magento\Framework\Session\Generic $session, | |
\StripeIntegration\Payments\Model\Config $config, | |
\Magento\Checkout\Model\Cart $cart, | |
\Magento\Framework\Locale\Resolver $localeResolver | |
) { | |
$this->helper = $helper; | |
$this->session = $session; | |
$this->config = $config; | |
$this->cart = $cart; | |
$this->localeResolver = $localeResolver; | |
} | |
public function getOrderItems($quote) | |
{ | |
$items = []; | |
$useStoreCurrency = $this->config->getConfigData('use_store_currency'); | |
$tax = 0; | |
$discount = 0; | |
$shipping = 0; | |
if ($useStoreCurrency) | |
{ | |
$currency = $quote->getQuoteCurrencyCode(); | |
if (!$quote->getIsVirtual()) | |
{ | |
$shippingAddress = $quote->getShippingAddress(); | |
$shipping = $shippingAddress->getShippingAmount(); | |
$tax += $shippingAddress->getShippingTaxAmount(); | |
} | |
$discount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount(); | |
} | |
else | |
{ | |
$currency = $quote->getBaseCurrencyCode(); | |
if (!$quote->getIsVirtual()) | |
{ | |
$shippingAddress = $quote->getShippingAddress(); | |
$shipping = $shippingAddress->getBaseShippingAmount(); | |
$tax += $shippingAddress->getBaseShippingTaxAmount(); | |
} | |
$discount = $quote->getBaseSubtotal() - $quote->getBaseSubtotalWithDiscount(); | |
} | |
$cents = $this->helper->isZeroDecimal($currency) ? 1 : 100; | |
$quoteItems = $quote->getAllVisibleItems(); | |
foreach ($quoteItems as $item) | |
{ | |
if ($useStoreCurrency) | |
{ | |
$amount = $item->getRowTotal(); | |
$tax += $item->getTaxAmount(); | |
} | |
else | |
{ | |
$amount = $item->getBaseRowTotal(); | |
$tax += $item->getBaseTaxAmount(); | |
} | |
$items[] = [ | |
"type" => "sku", | |
"parent" => $item->getSku(), | |
"description" => $item->getName(), | |
"quantity" => $item->getQty(), | |
"currency" => $currency, | |
"amount" => round($amount * $cents) | |
]; | |
} | |
if ($tax > 0) | |
{ | |
$items[] = [ | |
"type" => "tax", | |
"description" => "Tax", | |
"currency" => $currency, | |
"amount" => round($tax * $cents) | |
]; | |
} | |
if ($discount > 0) | |
{ | |
$items[] = [ | |
"type" => "discount", | |
"description" => "Discount", | |
"currency" => $currency, | |
"amount" => -round($discount * $cents) | |
]; | |
} | |
if ($shipping > 0) | |
{ | |
$items[] = [ | |
"type" => "shipping", | |
"description" => "Shipping", | |
"currency" => $currency, | |
"amount" => round($shipping * $cents) | |
]; | |
} | |
return $items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment