Button.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Block\Minicart;
  17. use Magento\Checkout\Model\Session;
  18. use Amazon\Payment\Helper\Data;
  19. use Amazon\Core\Helper\Data as AmazonCoreHelper;
  20. use Amazon\Payment\Gateway\Config\Config;
  21. use Magento\Paypal\Block\Express\InContext;
  22. use Magento\Framework\View\Element\Template;
  23. use Magento\Catalog\Block\ShortcutInterface;
  24. use Magento\Framework\Locale\ResolverInterface;
  25. use Magento\Framework\View\Element\Template\Context;
  26. use Magento\Framework\App\Request\Http;
  27. /**
  28. * Class Button
  29. *
  30. * @api
  31. */
  32. class Button extends Template implements ShortcutInterface
  33. {
  34. const ALIAS_ELEMENT_INDEX = 'alias';
  35. const CART_BUTTON_ELEMENT_INDEX = 'add_to_cart_selector';
  36. /**
  37. * @var bool
  38. */
  39. private $isMiniCart = false;
  40. /**
  41. * @var ResolverInterface
  42. */
  43. private $localeResolver;
  44. /**
  45. * @var Data
  46. */
  47. private $mainHelper;
  48. /**
  49. * @var Config
  50. */
  51. private $payment;
  52. /**
  53. * @var Session
  54. */
  55. private $session;
  56. /**
  57. * @var AmazonCoreHelper
  58. */
  59. private $coreHelper;
  60. /**
  61. * @var Http
  62. */
  63. private $request;
  64. /**
  65. * Button constructor.
  66. *
  67. * @param Context $context
  68. * @param ResolverInterface $localeResolver
  69. * @param Data $mainHelper
  70. * @param Session $session
  71. * @param Config $payment
  72. * @param AmazonCoreHelper $coreHelper
  73. * @param Http $request
  74. * @param array $data
  75. */
  76. public function __construct(
  77. Context $context,
  78. ResolverInterface $localeResolver,
  79. Data $mainHelper,
  80. Session $session,
  81. Config $payment,
  82. AmazonCoreHelper $coreHelper,
  83. Http $request,
  84. array $data = []
  85. ) {
  86. parent::__construct($context, $data);
  87. $this->localeResolver = $localeResolver;
  88. $this->mainHelper = $mainHelper;
  89. $this->payment = $payment;
  90. $this->session = $session;
  91. $this->coreHelper = $coreHelper;
  92. $this->request = $request;
  93. $this->payment->setMethodCode($this->payment::CODE);
  94. }
  95. /**
  96. * @return bool
  97. */
  98. protected function shouldRender()
  99. {
  100. if ($this->getIsCart() && $this->payment->isActive($this->session->getQuote()->getStoreId())) {
  101. return true;
  102. }
  103. return $this->coreHelper->isPayButtonAvailableInMinicart()
  104. && $this->payment->isActive($this->session->getQuote()->getStoreId())
  105. && $this->isMiniCart;
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. protected function _toHtml()
  111. {
  112. if (!$this->shouldRender()) {
  113. return '';
  114. }
  115. return parent::_toHtml();
  116. }
  117. protected function _isOnCartPage()
  118. {
  119. return $this->request->getFullActionName() == 'checkout_cart_index';
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getAddToCartSelector()
  125. {
  126. return $this->getData(self::CART_BUTTON_ELEMENT_INDEX);
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getImageUrl()
  132. {
  133. return $this->config->getExpressCheckoutInContextImageUrl(
  134. $this->localeResolver->getLocale()
  135. );
  136. }
  137. /**
  138. * Get shortcut alias
  139. *
  140. * @return string
  141. */
  142. public function getAlias()
  143. {
  144. return $this->getData(self::ALIAS_ELEMENT_INDEX);
  145. }
  146. /**
  147. * @param bool $isCatalog
  148. * @return $this
  149. */
  150. public function setIsInCatalogProduct($isCatalog)
  151. {
  152. $this->isMiniCart = !$isCatalog;
  153. return $this;
  154. }
  155. }