123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Helper;
- use Magento\Framework\Pricing\PriceCurrencyInterface;
- use Magento\Quote\Model\Quote\Item\AbstractItem;
- use Magento\Store\Model\Store;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Sales\Api\PaymentFailuresInterface;
- /**
- * Checkout default helper
- *
- * @author Magento Core Team <core@magentocommerce.com>
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Data extends \Magento\Framework\App\Helper\AbstractHelper
- {
- const XML_PATH_GUEST_CHECKOUT = 'checkout/options/guest_checkout';
- const XML_PATH_CUSTOMER_MUST_BE_LOGGED = 'checkout/options/customer_must_be_logged';
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Checkout\Model\Session
- */
- protected $_checkoutSession;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
- */
- protected $_localeDate;
- /**
- * @var \Magento\Framework\Mail\Template\TransportBuilder
- */
- protected $_transportBuilder;
- /**
- * @var \Magento\Framework\Translate\Inline\StateInterface
- */
- protected $inlineTranslation;
- /**
- * @var PriceCurrencyInterface
- */
- protected $priceCurrency;
- /**
- * @var PaymentFailuresInterface
- */
- private $paymentFailures;
- /**
- * Data constructor.
- *
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
- * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
- * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
- * @param PriceCurrencyInterface $priceCurrency
- * @param PaymentFailuresInterface|null $paymentFailures
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Framework\App\Helper\Context $context,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Checkout\Model\Session $checkoutSession,
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
- \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
- \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
- PriceCurrencyInterface $priceCurrency,
- PaymentFailuresInterface $paymentFailures = null
- ) {
- $this->_storeManager = $storeManager;
- $this->_checkoutSession = $checkoutSession;
- $this->_localeDate = $localeDate;
- $this->_transportBuilder = $transportBuilder;
- $this->inlineTranslation = $inlineTranslation;
- $this->priceCurrency = $priceCurrency;
- $this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
- ->get(PaymentFailuresInterface::class);
- parent::__construct($context);
- }
- /**
- * Retrieve checkout session model
- *
- * @return \Magento\Checkout\Model\Session
- * @codeCoverageIgnore
- */
- public function getCheckout()
- {
- return $this->_checkoutSession;
- }
- /**
- * Retrieve checkout quote model object
- *
- * @return \Magento\Quote\Model\Quote
- * @codeCoverageIgnore
- */
- public function getQuote()
- {
- return $this->getCheckout()->getQuote();
- }
- /**
- * Format Price
- *
- * @param float $price
- * @return string
- */
- public function formatPrice($price)
- {
- return $this->priceCurrency->format(
- $price,
- true,
- PriceCurrencyInterface::DEFAULT_PRECISION,
- $this->getQuote()->getStore()
- );
- }
- /**
- * Convert Price
- *
- * @param float $price
- * @param bool $format
- * @return float
- */
- public function convertPrice($price, $format = true)
- {
- return $format
- ? $this->priceCurrency->convertAndFormat($price)
- : $this->priceCurrency->convert($price);
- }
- /**
- * Get onepage checkout availability
- *
- * @return bool
- */
- public function canOnepageCheckout()
- {
- return $this->scopeConfig->isSetFlag(
- 'checkout/options/onepage_checkout_enabled',
- ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get sales item (quote item, order item etc) price including tax based on row total and tax amount
- *
- * @param \Magento\Framework\DataObject $item
- * @return float
- */
- public function getPriceInclTax($item)
- {
- if ($item->getPriceInclTax()) {
- return $item->getPriceInclTax();
- }
- $qty = $item->getQty() ? $item->getQty() : ($item->getQtyOrdered() ? $item->getQtyOrdered() : 1);
- $taxAmount = $item->getTaxAmount() + $item->getDiscountTaxCompensation();
- $price = (float)$qty ? ($item->getRowTotal() + $taxAmount) / $qty : 0;
- return $this->priceCurrency->round($price);
- }
- /**
- * Get sales item (quote item, order item etc) row total price including tax
- *
- * @param \Magento\Framework\DataObject $item
- * @return float
- */
- public function getSubtotalInclTax($item)
- {
- if ($item->getRowTotalInclTax()) {
- return $item->getRowTotalInclTax();
- }
- $tax = $item->getTaxAmount() + $item->getDiscountTaxCompensation();
- return $item->getRowTotal() + $tax;
- }
- /**
- * Get Base Price Incl Tax
- *
- * @param AbstractItem $item
- * @return float
- */
- public function getBasePriceInclTax($item)
- {
- $qty = $item->getQty() ? $item->getQty() : ($item->getQtyOrdered() ? $item->getQtyOrdered() : 1);
- $taxAmount = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
- $price = (float)$qty ? ($item->getBaseRowTotal() + $taxAmount) / $qty : 0;
- return $this->priceCurrency->round($price);
- }
- /**
- * Get Base Subtotal Incl Tax
- *
- * @param AbstractItem $item
- * @return float
- */
- public function getBaseSubtotalInclTax($item)
- {
- $tax = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
- return $item->getBaseRowTotal() + $tax;
- }
- /**
- * Send email id payment was failed
- *
- * @param \Magento\Quote\Model\Quote $checkout
- * @param string $message
- * @param string $checkoutType
- * @return $this
- */
- public function sendPaymentFailedEmail(
- \Magento\Quote\Model\Quote $checkout,
- string $message,
- string $checkoutType = 'onepage'
- ): Data {
- $this->paymentFailures->handle((int)$checkout->getId(), $message, $checkoutType);
- return $this;
- }
- /**
- * Get Emails
- *
- * @param string $configPath
- * @param null|string|bool|int|Store $storeId
- * @return array|false
- */
- protected function _getEmails($configPath, $storeId)
- {
- $data = $this->scopeConfig->getValue(
- $configPath,
- ScopeInterface::SCOPE_STORE,
- $storeId
- );
- if (!empty($data)) {
- return explode(',', $data);
- }
- return false;
- }
- /**
- * Check is allowed Guest Checkout. Use config settings and observer
- *
- * @param \Magento\Quote\Model\Quote $quote
- * @param int|Store $store
- * @return bool
- */
- public function isAllowedGuestCheckout(\Magento\Quote\Model\Quote $quote, $store = null)
- {
- if ($store === null) {
- $store = $quote->getStoreId();
- }
- $guestCheckout = $this->scopeConfig->isSetFlag(
- self::XML_PATH_GUEST_CHECKOUT,
- ScopeInterface::SCOPE_STORE,
- $store
- );
- if ($guestCheckout == true) {
- $result = new \Magento\Framework\DataObject();
- $result->setIsAllowed($guestCheckout);
- $this->_eventManager->dispatch(
- 'checkout_allow_guest',
- ['quote' => $quote, 'store' => $store, 'result' => $result]
- );
- $guestCheckout = $result->getIsAllowed();
- }
- return $guestCheckout;
- }
- /**
- * Check if context is checkout
- *
- * @return bool
- * @codeCoverageIgnore
- */
- public function isContextCheckout()
- {
- return $this->_request->getParam('context') == 'checkout';
- }
- /**
- * Check if user must be logged during checkout process
- *
- * @return boolean
- * @codeCoverageIgnore
- */
- public function isCustomerMustBeLogged()
- {
- return $this->scopeConfig->isSetFlag(
- self::XML_PATH_CUSTOMER_MUST_BE_LOGGED,
- ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * If display billing address on payment method is available, otherwise should be display on payment page
- *
- * @return bool
- */
- public function isDisplayBillingOnPaymentMethodAvailable()
- {
- return (bool) !$this->scopeConfig->getValue(
- 'checkout/options/display_billing_address_on',
- ScopeInterface::SCOPE_STORE
- );
- }
- }
|