Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Button.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Paypal\Model\ConfigFactory;
17 
21 class Button extends Template implements ShortcutInterface
22 {
23  const ALIAS_ELEMENT_INDEX = 'alias';
24 
25  const PAYPAL_BUTTON_ID = 'paypal-express-in-context-checkout-main';
26 
27  const BUTTON_ELEMENT_INDEX = 'button_id';
28 
29  const LINK_DATA_ACTION = 'link_data_action';
30 
31  const CART_BUTTON_ELEMENT_INDEX = 'add_to_cart_selector';
32 
36  private $isMiniCart = false;
37 
41  private $localeResolver;
42 
46  private $config;
47 
51  private $payment;
52 
56  private $session;
57 
66  public function __construct(
67  Context $context,
68  ResolverInterface $localeResolver,
69  ConfigFactory $configFactory,
70  Session $session,
71  MethodInterface $payment,
72  array $data = []
73  ) {
74  parent::__construct($context, $data);
75 
76  $this->localeResolver = $localeResolver;
77  $this->config = $configFactory->create();
78  $this->config->setMethod(Config::METHOD_EXPRESS);
79  $this->payment = $payment;
80  $this->session = $session;
81  }
82 
88  private function isInContext()
89  {
90  return (bool)(int) $this->config->getValue('in_context');
91  }
92 
98  private function isVisibleOnCart()
99  {
100  return (bool)(int) $this->config->getValue('visible_on_cart');
101  }
102 
109  protected function shouldRender()
110  {
111  return $this->payment->isAvailable($this->session->getQuote())
112  && $this->isMiniCart
113  && $this->isInContext()
114  && $this->isVisibleOnCart();
115  }
116 
120  protected function _toHtml()
121  {
122  if (!$this->shouldRender()) {
123  return '';
124  }
125 
126  return parent::_toHtml();
127  }
128 
132  public function getContainerId()
133  {
134  return $this->getData(self::BUTTON_ELEMENT_INDEX);
135  }
136 
140  public function getLinkAction()
141  {
142  return $this->getData(self::LINK_DATA_ACTION);
143  }
144 
148  public function getAddToCartSelector()
149  {
150  return $this->getData(self::CART_BUTTON_ELEMENT_INDEX);
151  }
152 
156  public function getImageUrl()
157  {
158  return $this->config->getExpressCheckoutInContextImageUrl(
159  $this->localeResolver->getLocale()
160  );
161  }
162 
168  public function getAlias()
169  {
170  return $this->getData(self::ALIAS_ELEMENT_INDEX);
171  }
172 
177  public function setIsInCatalogProduct($isCatalog)
178  {
179  $this->isMiniCart = !$isCatalog;
180 
181  return $this;
182  }
183 }
getData($key='', $index=null)
Definition: DataObject.php:119
$payment
Definition: order.php:17
__construct(Context $context, ResolverInterface $localeResolver, ConfigFactory $configFactory, Session $session, MethodInterface $payment, array $data=[])
Definition: Button.php:66
$configFactory
Definition: config_data.php:43