Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Shortcut.php
Go to the documentation of this file.
1 <?php
7 
11 
18 {
24  protected $_shouldRender = true;
25 
31  protected $_paymentMethodCode = '';
32 
38  protected $_startAction = '';
39 
45  protected $_checkoutType = '';
46 
52  protected $_alias = '';
53 
58 
62  protected $_checkoutSession;
63 
67  protected $_checkoutFactory;
68 
72  protected $_mathRandom;
73 
77  protected $_localeResolver;
78 
82  private $_shortcutValidator;
83 
87  private $config;
88 
105  public function __construct(
106  \Magento\Framework\View\Element\Template\Context $context,
107  \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory,
108  \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
109  \Magento\Framework\Math\Random $mathRandom,
110  \Magento\Framework\Locale\ResolverInterface $localeResolver,
111  ValidatorInterface $shortcutValidator,
112  $paymentMethodCode,
113  $startAction,
114  $checkoutType,
115  $alias,
116  $shortcutTemplate,
117  \Magento\Checkout\Model\Session $checkoutSession = null,
118  array $data = []
119  ) {
120  $this->_paypalConfigFactory = $paypalConfigFactory;
121  $this->_checkoutSession = $checkoutSession;
122  $this->_checkoutFactory = $checkoutFactory;
123  $this->_mathRandom = $mathRandom;
124  $this->_localeResolver = $localeResolver;
125  $this->_shortcutValidator = $shortcutValidator;
126 
127  $this->_paymentMethodCode = $paymentMethodCode;
128  $this->_startAction = $startAction;
129  $this->_checkoutType = $checkoutType;
130  $this->_alias = $alias;
131  $this->setTemplate($shortcutTemplate);
132 
133  parent::__construct($context, $data);
134 
135  $this->config = $this->_paypalConfigFactory->create();
136  $this->config->setMethod($this->_paymentMethodCode);
137  }
138 
142  protected function _beforeToHtml()
143  {
144  $result = parent::_beforeToHtml();
145 
146  $isInCatalog = $this->getIsInCatalogProduct();
147 
148  if (!$this->_shortcutValidator->validate($this->_paymentMethodCode, $isInCatalog)) {
149  $this->_shouldRender = false;
150  return $result;
151  }
152 
153  $quote = $isInCatalog || !$this->_checkoutSession ? null : $this->_checkoutSession->getQuote();
154 
155  // set misc data
156  $this->setShortcutHtmlId(
157  $this->_mathRandom->getUniqueHash('ec_shortcut_')
158  )->setCheckoutUrl(
159  $this->getUrl($this->_startAction)
160  );
161 
162  // use static image if in catalog
163  if ($isInCatalog || null === $quote) {
164  $this->setImageUrl($this->config->getExpressCheckoutShortcutImageUrl($this->_localeResolver->getLocale()));
165  } else {
167  $parameters = ['params' => ['quote' => $quote, 'config' => $this->config]];
168  $checkoutModel = $this->_checkoutFactory->create($this->_checkoutType, $parameters);
169  $this->setImageUrl($checkoutModel->getCheckoutShortcutImageUrl());
170  }
171 
172  return $result;
173  }
174 
180  protected function _toHtml()
181  {
182  if (!$this->shouldRender()) {
183  return '';
184  }
185  return parent::_toHtml();
186  }
187 
191  protected function shouldRender()
192  {
193  $this->config->setMethod(Config::METHOD_EXPRESS);
194 
195  $isInCatalog = $this->getIsInCatalogProduct();
196  $isInContext = (bool)(int) $this->config->getValue('in_context');
197 
198  $isEnabled = ($isInContext && $isInCatalog) || !$isInContext;
199  $this->config->setMethod($this->_paymentMethodCode);
200 
201  return $isEnabled && $this->_shouldRender;
202  }
203 
209  public function isOrPositionBefore()
210  {
211  return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_BEFORE;
212  }
213 
219  public function isOrPositionAfter()
220  {
221  return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_AFTER;
222  }
223 
229  public function getAlias()
230  {
231  return $this->_alias;
232  }
233 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory, \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory, \Magento\Framework\Math\Random $mathRandom, \Magento\Framework\Locale\ResolverInterface $localeResolver, ValidatorInterface $shortcutValidator, $paymentMethodCode, $startAction, $checkoutType, $alias, $shortcutTemplate, \Magento\Checkout\Model\Session $checkoutSession=null, array $data=[])
Definition: Shortcut.php:105
$quote
if(!trim($html)) $alias
Definition: details.phtml:20