Button.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Express\InContext\Minicart;
  7. use Magento\Checkout\Model\Session;
  8. use Magento\Payment\Model\MethodInterface;
  9. use Magento\Paypal\Model\Config;
  10. use Magento\Paypal\Model\ConfigFactory;
  11. use Magento\Framework\View\Element\Template;
  12. use Magento\Catalog\Block\ShortcutInterface;
  13. use Magento\Framework\Locale\ResolverInterface;
  14. use Magento\Framework\View\Element\Template\Context;
  15. /**
  16. * Class Button
  17. * @deprecated 100.3.1 @see \Magento\Paypal\Block\Express\InContext\Minicart\SmartButton
  18. */
  19. class Button extends Template implements ShortcutInterface
  20. {
  21. const ALIAS_ELEMENT_INDEX = 'alias';
  22. const PAYPAL_BUTTON_ID = 'paypal-express-in-context-checkout-main';
  23. const BUTTON_ELEMENT_INDEX = 'button_id';
  24. const LINK_DATA_ACTION = 'link_data_action';
  25. const CART_BUTTON_ELEMENT_INDEX = 'add_to_cart_selector';
  26. /**
  27. * @var bool
  28. */
  29. private $isMiniCart = false;
  30. /**
  31. * @var ResolverInterface
  32. */
  33. private $localeResolver;
  34. /**
  35. * @var Config
  36. */
  37. private $config;
  38. /**
  39. * @var MethodInterface
  40. */
  41. private $payment;
  42. /**
  43. * @var Session
  44. */
  45. private $session;
  46. /**
  47. * @param Context $context
  48. * @param ResolverInterface $localeResolver
  49. * @param ConfigFactory $configFactory
  50. * @param Session $session
  51. * @param MethodInterface $payment
  52. * @param array $data
  53. */
  54. public function __construct(
  55. Context $context,
  56. ResolverInterface $localeResolver,
  57. ConfigFactory $configFactory,
  58. Session $session,
  59. MethodInterface $payment,
  60. array $data = []
  61. ) {
  62. parent::__construct($context, $data);
  63. $this->localeResolver = $localeResolver;
  64. $this->config = $configFactory->create();
  65. $this->config->setMethod(Config::METHOD_EXPRESS);
  66. $this->payment = $payment;
  67. $this->session = $session;
  68. }
  69. /**
  70. * Check `in_context` config value
  71. *
  72. * @return bool
  73. */
  74. private function isInContext()
  75. {
  76. return (bool)(int) $this->config->getValue('in_context');
  77. }
  78. /**
  79. * Check `visible_on_cart` config value
  80. *
  81. * @return bool
  82. */
  83. private function isVisibleOnCart()
  84. {
  85. return (bool)(int) $this->config->getValue('visible_on_cart');
  86. }
  87. /**
  88. * Check is Paypal In-Context Express Checkout button should render in cart/mini-cart
  89. *
  90. * @return bool
  91. */
  92. protected function shouldRender()
  93. {
  94. return $this->payment->isAvailable($this->session->getQuote())
  95. && $this->isMiniCart
  96. && $this->isInContext()
  97. && $this->isVisibleOnCart();
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. protected function _toHtml()
  103. {
  104. if (!$this->shouldRender()) {
  105. return '';
  106. }
  107. return parent::_toHtml();
  108. }
  109. /**
  110. * Returns container id
  111. *
  112. * @return string
  113. */
  114. public function getContainerId()
  115. {
  116. return $this->getData(self::BUTTON_ELEMENT_INDEX);
  117. }
  118. /**
  119. * Returns link action
  120. *
  121. * @return string
  122. */
  123. public function getLinkAction()
  124. {
  125. return $this->getData(self::LINK_DATA_ACTION);
  126. }
  127. /**
  128. * Returns add to cart selector
  129. *
  130. * @return string
  131. */
  132. public function getAddToCartSelector()
  133. {
  134. return $this->getData(self::CART_BUTTON_ELEMENT_INDEX);
  135. }
  136. /**
  137. * Returns image url
  138. *
  139. * @return string
  140. */
  141. public function getImageUrl()
  142. {
  143. return $this->config->getExpressCheckoutInContextImageUrl(
  144. $this->localeResolver->getLocale()
  145. );
  146. }
  147. /**
  148. * Get shortcut alias
  149. *
  150. * @return string
  151. */
  152. public function getAlias()
  153. {
  154. return $this->getData(self::ALIAS_ELEMENT_INDEX);
  155. }
  156. /**
  157. * Set information if button renders in the mini cart
  158. *
  159. * @param bool $isCatalog
  160. * @return $this
  161. */
  162. public function setIsInCatalogProduct($isCatalog)
  163. {
  164. $this->isMiniCart = !$isCatalog;
  165. return $this;
  166. }
  167. }