123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\CustomerData;
- use Magento\Customer\CustomerData\SectionSourceInterface;
- /**
- * Cart source
- *
- * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
- */
- class Cart extends \Magento\Framework\DataObject implements SectionSourceInterface
- {
- /**
- * @var \Magento\Customer\Model\Session
- */
- protected $checkoutSession;
- /**
- * @var \Magento\Checkout\Model\Cart
- */
- protected $checkoutCart;
- /**
- * @var \Magento\Catalog\Model\ResourceModel\Url
- */
- protected $catalogUrl;
- /**
- * @var \Magento\Quote\Model\Quote|null
- */
- protected $quote = null;
- /**
- * @var \Magento\Checkout\Helper\Data
- */
- protected $checkoutHelper;
- /**
- * @var ItemPoolInterface
- */
- protected $itemPoolInterface;
- /**
- * @var int|float
- */
- protected $summeryCount;
- /**
- * @var \Magento\Framework\View\LayoutInterface
- */
- protected $layout;
- /**
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
- * @param \Magento\Checkout\Model\Cart $checkoutCart
- * @param \Magento\Checkout\Helper\Data $checkoutHelper
- * @param ItemPoolInterface $itemPoolInterface
- * @param \Magento\Framework\View\LayoutInterface $layout
- * @param array $data
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Checkout\Model\Session $checkoutSession,
- \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
- \Magento\Checkout\Model\Cart $checkoutCart,
- \Magento\Checkout\Helper\Data $checkoutHelper,
- ItemPoolInterface $itemPoolInterface,
- \Magento\Framework\View\LayoutInterface $layout,
- array $data = []
- ) {
- parent::__construct($data);
- $this->checkoutSession = $checkoutSession;
- $this->catalogUrl = $catalogUrl;
- $this->checkoutCart = $checkoutCart;
- $this->checkoutHelper = $checkoutHelper;
- $this->itemPoolInterface = $itemPoolInterface;
- $this->layout = $layout;
- }
- /**
- * @inheritdoc
- */
- public function getSectionData()
- {
- $totals = $this->getQuote()->getTotals();
- $subtotalAmount = $totals['subtotal']->getValue();
- return [
- 'summary_count' => $this->getSummaryCount(),
- 'subtotalAmount' => $subtotalAmount,
- 'subtotal' => isset($totals['subtotal'])
- ? $this->checkoutHelper->formatPrice($subtotalAmount)
- : 0,
- 'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
- 'items' => $this->getRecentItems(),
- 'extra_actions' => $this->layout->createBlock(\Magento\Catalog\Block\ShortcutButtons::class)->toHtml(),
- 'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
- 'website_id' => $this->getQuote()->getStore()->getWebsiteId(),
- 'storeId' => $this->getQuote()->getStore()->getStoreId()
- ];
- }
- /**
- * Get active quote
- *
- * @return \Magento\Quote\Model\Quote
- */
- protected function getQuote()
- {
- if (null === $this->quote) {
- $this->quote = $this->checkoutSession->getQuote();
- }
- return $this->quote;
- }
- /**
- * Get shopping cart items qty based on configuration (summary qty or items qty)
- *
- * @return int|float
- */
- protected function getSummaryCount()
- {
- if (!$this->summeryCount) {
- $this->summeryCount = $this->checkoutCart->getSummaryQty() ?: 0;
- }
- return $this->summeryCount;
- }
- /**
- * Check if one page checkout is available
- *
- * @return bool
- */
- protected function isPossibleOnepageCheckout()
- {
- return $this->checkoutHelper->canOnepageCheckout() && !$this->getQuote()->getHasError();
- }
- /**
- * Get array of last added items
- *
- * @return \Magento\Quote\Model\Quote\Item[]
- */
- protected function getRecentItems()
- {
- $items = [];
- if (!$this->getSummaryCount()) {
- return $items;
- }
- foreach (array_reverse($this->getAllQuoteItems()) as $item) {
- /* @var $item \Magento\Quote\Model\Quote\Item */
- if (!$item->getProduct()->isVisibleInSiteVisibility()) {
- $product = $item->getOptionByCode('product_type') !== null
- ? $item->getOptionByCode('product_type')->getProduct()
- : $item->getProduct();
- $products = $this->catalogUrl->getRewriteByProductStore([$product->getId() => $item->getStoreId()]);
- if (isset($products[$product->getId()])) {
- $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
- $item->getProduct()->setUrlDataObject($urlDataObject);
- }
- }
- $items[] = $this->itemPoolInterface->getItemData($item);
- }
- return $items;
- }
- /**
- * Return customer quote items
- *
- * @return \Magento\Quote\Model\Quote\Item[]
- */
- protected function getAllQuoteItems()
- {
- if ($this->getCustomQuote()) {
- return $this->getCustomQuote()->getAllVisibleItems();
- }
- return $this->getQuote()->getAllVisibleItems();
- }
- /**
- * Check if guest checkout is allowed
- *
- * @return bool
- */
- public function isGuestCheckoutAllowed()
- {
- return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
- }
- }
|