123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Block;
- use Magento\Customer\Model\Context;
- /**
- * Shopping cart block
- *
- * @api
- * @since 100.0.2
- */
- class Cart extends \Magento\Checkout\Block\Cart\AbstractCart
- {
- /**
- * @var \Magento\Catalog\Model\ResourceModel\Url
- */
- protected $_catalogUrlBuilder;
- /**
- * @var \Magento\Framework\App\Http\Context
- */
- protected $httpContext;
- /**
- * @var \Magento\Checkout\Helper\Cart
- */
- protected $_cartHelper;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder
- * @param \Magento\Checkout\Helper\Cart $cartHelper
- * @param \Magento\Framework\App\Http\Context $httpContext
- * @param array $data
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Customer\Model\Session $customerSession,
- \Magento\Checkout\Model\Session $checkoutSession,
- \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder,
- \Magento\Checkout\Helper\Cart $cartHelper,
- \Magento\Framework\App\Http\Context $httpContext,
- array $data = []
- ) {
- $this->_cartHelper = $cartHelper;
- $this->_catalogUrlBuilder = $catalogUrlBuilder;
- parent::__construct($context, $customerSession, $checkoutSession, $data);
- $this->_isScopePrivate = true;
- $this->httpContext = $httpContext;
- }
- /**
- * Prepare Quote Item Product URLs
- *
- * @codeCoverageIgnore
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->prepareItemUrls();
- }
- /**
- * prepare cart items URLs
- *
- * @return void
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function prepareItemUrls()
- {
- $products = [];
- /* @var $item \Magento\Quote\Model\Quote\Item */
- foreach ($this->getItems() as $item) {
- $product = $item->getProduct();
- $option = $item->getOptionByCode('product_type');
- if ($option) {
- $product = $option->getProduct();
- }
- if ($item->getStoreId() != $this->_storeManager->getStore()->getId() &&
- !$item->getRedirectUrl() &&
- !$product->isVisibleInSiteVisibility()
- ) {
- $products[$product->getId()] = $item->getStoreId();
- }
- }
- if ($products) {
- $products = $this->_catalogUrlBuilder->getRewriteByProductStore($products);
- foreach ($this->getItems() as $item) {
- $product = $item->getProduct();
- $option = $item->getOptionByCode('product_type');
- if ($option) {
- $product = $option->getProduct();
- }
- if (isset($products[$product->getId()])) {
- $object = new \Magento\Framework\DataObject($products[$product->getId()]);
- $item->getProduct()->setUrlDataObject($object);
- }
- }
- }
- }
- /**
- * @codeCoverageIgnore
- * @return bool
- */
- public function hasError()
- {
- return $this->getQuote()->getHasError();
- }
- /**
- * @codeCoverageIgnore
- * @return int
- */
- public function getItemsSummaryQty()
- {
- return $this->getQuote()->getItemsSummaryQty();
- }
- /**
- * @codeCoverageIgnore
- * @return bool
- */
- public function isWishlistActive()
- {
- $isActive = $this->_getData('is_wishlist_active');
- if ($isActive === null) {
- $isActive = $this->_scopeConfig->getValue(
- 'wishlist/general/active',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- ) && $this->httpContext->getValue(
- Context::CONTEXT_AUTH
- );
- $this->setIsWishlistActive($isActive);
- }
- return $isActive;
- }
- /**
- * @codeCoverageIgnore
- * @return string
- */
- public function getCheckoutUrl()
- {
- return $this->getUrl('checkout', ['_secure' => true]);
- }
- /**
- * @return string
- */
- public function getContinueShoppingUrl()
- {
- $url = $this->getData('continue_shopping_url');
- if ($url === null) {
- $url = $this->_checkoutSession->getContinueShoppingUrl(true);
- if (!$url) {
- $url = $this->_urlBuilder->getUrl();
- }
- $this->setData('continue_shopping_url', $url);
- }
- return $url;
- }
- /**
- * @return bool
- * @codeCoverageIgnore
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getIsVirtual()
- {
- return $this->_cartHelper->getIsVirtualQuote();
- }
- /**
- * Return list of available checkout methods
- *
- * @param string $alias Container block alias in layout
- * @return array
- */
- public function getMethods($alias)
- {
- $childName = $this->getLayout()->getChildName($this->getNameInLayout(), $alias);
- if ($childName) {
- return $this->getLayout()->getChildNames($childName);
- }
- return [];
- }
- /**
- * Return HTML of checkout method (link, button etc.)
- *
- * @param string $name Block name in layout
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getMethodHtml($name)
- {
- $block = $this->getLayout()->getBlock($name);
- if (!$block) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Invalid method: %1', $name));
- }
- return $block->toHtml();
- }
- /**
- * Return customer quote items
- *
- * @return array
- */
- public function getItems()
- {
- if ($this->getCustomItems()) {
- return $this->getCustomItems();
- }
- return parent::getItems();
- }
- /**
- * @codeCoverageIgnore
- * @return int
- */
- public function getItemsCount()
- {
- return $this->getQuote()->getItemsCount();
- }
- /**
- * Render pagination HTML
- *
- * @return string
- * @since 100.1.7
- */
- public function getPagerHtml()
- {
- return $this->getChildHtml('pager');
- }
- }
|