Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Checkout\Block;
7 
9 
16 class Cart extends \Magento\Checkout\Block\Cart\AbstractCart
17 {
22 
26  protected $httpContext;
27 
31  protected $_cartHelper;
32 
43  public function __construct(
44  \Magento\Framework\View\Element\Template\Context $context,
45  \Magento\Customer\Model\Session $customerSession,
46  \Magento\Checkout\Model\Session $checkoutSession,
47  \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder,
48  \Magento\Checkout\Helper\Cart $cartHelper,
49  \Magento\Framework\App\Http\Context $httpContext,
50  array $data = []
51  ) {
52  $this->_cartHelper = $cartHelper;
53  $this->_catalogUrlBuilder = $catalogUrlBuilder;
54  parent::__construct($context, $customerSession, $checkoutSession, $data);
55  $this->_isScopePrivate = true;
56  $this->httpContext = $httpContext;
57  }
58 
65  protected function _construct()
66  {
67  parent::_construct();
68  $this->prepareItemUrls();
69  }
70 
77  public function prepareItemUrls()
78  {
79  $products = [];
80  /* @var $item \Magento\Quote\Model\Quote\Item */
81  foreach ($this->getItems() as $item) {
82  $product = $item->getProduct();
83  $option = $item->getOptionByCode('product_type');
84  if ($option) {
85  $product = $option->getProduct();
86  }
87 
88  if ($item->getStoreId() != $this->_storeManager->getStore()->getId() &&
89  !$item->getRedirectUrl() &&
90  !$product->isVisibleInSiteVisibility()
91  ) {
92  $products[$product->getId()] = $item->getStoreId();
93  }
94  }
95 
96  if ($products) {
97  $products = $this->_catalogUrlBuilder->getRewriteByProductStore($products);
98  foreach ($this->getItems() as $item) {
99  $product = $item->getProduct();
100  $option = $item->getOptionByCode('product_type');
101  if ($option) {
102  $product = $option->getProduct();
103  }
104 
105  if (isset($products[$product->getId()])) {
106  $object = new \Magento\Framework\DataObject($products[$product->getId()]);
107  $item->getProduct()->setUrlDataObject($object);
108  }
109  }
110  }
111  }
112 
117  public function hasError()
118  {
119  return $this->getQuote()->getHasError();
120  }
121 
126  public function getItemsSummaryQty()
127  {
128  return $this->getQuote()->getItemsSummaryQty();
129  }
130 
135  public function isWishlistActive()
136  {
137  $isActive = $this->_getData('is_wishlist_active');
138  if ($isActive === null) {
139  $isActive = $this->_scopeConfig->getValue(
140  'wishlist/general/active',
141  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
142  ) && $this->httpContext->getValue(
144  );
145  $this->setIsWishlistActive($isActive);
146  }
147  return $isActive;
148  }
149 
154  public function getCheckoutUrl()
155  {
156  return $this->getUrl('checkout', ['_secure' => true]);
157  }
158 
162  public function getContinueShoppingUrl()
163  {
164  $url = $this->getData('continue_shopping_url');
165  if ($url === null) {
166  $url = $this->_checkoutSession->getContinueShoppingUrl(true);
167  if (!$url) {
168  $url = $this->_urlBuilder->getUrl();
169  }
170  $this->setData('continue_shopping_url', $url);
171  }
172  return $url;
173  }
174 
180  public function getIsVirtual()
181  {
182  return $this->_cartHelper->getIsVirtualQuote();
183  }
184 
191  public function getMethods($alias)
192  {
193  $childName = $this->getLayout()->getChildName($this->getNameInLayout(), $alias);
194  if ($childName) {
195  return $this->getLayout()->getChildNames($childName);
196  }
197  return [];
198  }
199 
207  public function getMethodHtml($name)
208  {
209  $block = $this->getLayout()->getBlock($name);
210  if (!$block) {
211  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid method: %1', $name));
212  }
213  return $block->toHtml();
214  }
215 
221  public function getItems()
222  {
223  if ($this->getCustomItems()) {
224  return $this->getCustomItems();
225  }
226 
227  return parent::getItems();
228  }
229 
234  public function getItemsCount()
235  {
236  return $this->getQuote()->getItemsCount();
237  }
238 
245  public function getPagerHtml()
246  {
247  return $this->getChildHtml('pager');
248  }
249 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$block
Definition: block.php:8
setData($key, $value=null)
Definition: DataObject.php:72
__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=[])
Definition: Cart.php:43
if(!trim($html)) $alias
Definition: details.phtml:20
if(!isset($_GET['name'])) $name
Definition: log.php:14