_paypalConfigFactory = $paypalConfigFactory; $this->_checkoutSession = $checkoutSession; $this->_checkoutFactory = $checkoutFactory; $this->_mathRandom = $mathRandom; $this->_localeResolver = $localeResolver; $this->_shortcutValidator = $shortcutValidator; $this->_paymentMethodCode = $paymentMethodCode; $this->_startAction = $startAction; $this->_checkoutType = $checkoutType; $this->_alias = $alias; $this->setTemplate($shortcutTemplate); parent::__construct($context, $data); $this->config = $this->_paypalConfigFactory->create(); $this->config->setMethod($this->_paymentMethodCode); } /** * @return \Magento\Framework\View\Element\AbstractBlock */ protected function _beforeToHtml() { $result = parent::_beforeToHtml(); $isInCatalog = $this->getIsInCatalogProduct(); if (!$this->_shortcutValidator->validate($this->_paymentMethodCode, $isInCatalog)) { $this->_shouldRender = false; return $result; } $quote = $isInCatalog || !$this->_checkoutSession ? null : $this->_checkoutSession->getQuote(); // set misc data $this->setShortcutHtmlId( $this->_mathRandom->getUniqueHash('ec_shortcut_') )->setCheckoutUrl( $this->getUrl($this->_startAction) ); // use static image if in catalog if ($isInCatalog || null === $quote) { $this->setImageUrl($this->config->getExpressCheckoutShortcutImageUrl($this->_localeResolver->getLocale())); } else { /**@todo refactor checkout model. Move getCheckoutShortcutImageUrl to helper or separate model */ $parameters = ['params' => ['quote' => $quote, 'config' => $this->config]]; $checkoutModel = $this->_checkoutFactory->create($this->_checkoutType, $parameters); $this->setImageUrl($checkoutModel->getCheckoutShortcutImageUrl()); } return $result; } /** * Render the block if needed * * @return string */ protected function _toHtml() { if (!$this->shouldRender()) { return ''; } return parent::_toHtml(); } /** * @return bool */ protected function shouldRender() { $this->config->setMethod(Config::METHOD_EXPRESS); $isInCatalog = $this->getIsInCatalogProduct(); $isInContext = (bool)(int) $this->config->getValue('in_context'); $isEnabled = ($isInContext && $isInCatalog) || !$isInContext; $this->config->setMethod($this->_paymentMethodCode); return $isEnabled && $this->_shouldRender; } /** * Check is "OR" label position before shortcut * * @return bool */ public function isOrPositionBefore() { return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_BEFORE; } /** * Check is "OR" label position after shortcut * * @return bool */ public function isOrPositionAfter() { return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_AFTER; } /** * Get shortcut alias * * @return string */ public function getAlias() { return $this->_alias; } }