Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddPaypalShortcutsObserver.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  protected $shortcutFactory;
22 
26  protected $paypalConfig;
27 
34  public function __construct(
37  ) {
38  $this->shortcutFactory = $shortcutFactory;
39  $this->paypalConfig = $paypalConfig;
40  }
41 
48  public function execute(EventObserver $observer)
49  {
51  $shortcutButtons = $observer->getEvent()->getContainer();
52  $blocks = [
53  \Magento\Paypal\Block\Express\InContext\Minicart\Button::class =>
54  PaypalConfig::METHOD_WPS_EXPRESS,
55  \Magento\Paypal\Block\Express\Shortcut::class => PaypalConfig::METHOD_WPP_EXPRESS,
56  \Magento\Paypal\Block\Bml\Shortcut::class => PaypalConfig::METHOD_WPP_EXPRESS,
57  \Magento\Paypal\Block\WpsExpress\Shortcut::class => PaypalConfig::METHOD_WPS_EXPRESS,
58  \Magento\Paypal\Block\WpsBml\Shortcut::class => PaypalConfig::METHOD_WPS_EXPRESS,
59  \Magento\Paypal\Block\PayflowExpress\Shortcut::class => PaypalConfig::METHOD_WPP_PE_EXPRESS,
60  \Magento\Paypal\Block\Payflow\Bml\Shortcut::class => PaypalConfig::METHOD_WPP_PE_EXPRESS
61  ];
62  foreach ($blocks as $blockInstanceName => $paymentMethodCode) {
63  if (!$this->paypalConfig->isMethodAvailable($paymentMethodCode)) {
64  continue;
65  }
66 
67  $params = [
68  'shortcutValidator' => $this->shortcutFactory->create($observer->getEvent()->getCheckoutSession()),
69  ];
70  if (!in_array('Bml', explode('\\', $blockInstanceName))) {
71  $params['checkoutSession'] = $observer->getEvent()->getCheckoutSession();
72  }
73 
74  // we believe it's \Magento\Framework\View\Element\Template
75  $shortcut = $shortcutButtons->getLayout()->createBlock(
76  $blockInstanceName,
77  '',
78  $params
79  );
80  $shortcut->setIsInCatalogProduct(
81  $observer->getEvent()->getIsCatalogProduct()
82  )->setShowOrPosition(
83  $observer->getEvent()->getOrPosition()
84  );
85  $shortcutButtons->addShortcut($shortcut);
86  }
87  }
88 }
__construct(Factory $shortcutFactory, PaypalConfig $paypalConfig)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18