AddAmazonButton.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Observer;
  17. use Magento\Framework\Event\ObserverInterface;
  18. use Magento\Framework\Event\Observer;
  19. use Amazon\Core\Helper\Data;
  20. use Amazon\Payment\Helper\Shortcut\Factory as ShortcutFactory;
  21. class AddAmazonButton implements ObserverInterface
  22. {
  23. /**
  24. * @var Data
  25. */
  26. private $coreHelper;
  27. /**
  28. * @var ShortcutFactory
  29. */
  30. private $shortcutFactory;
  31. /**
  32. * @param Data $coreHelper
  33. * @param ShortcutFactory $shortcutFactory
  34. */
  35. public function __construct(
  36. Data $coreHelper,
  37. ShortcutFactory $shortcutFactory
  38. ) {
  39. $this->coreHelper = $coreHelper;
  40. $this->shortcutFactory = $shortcutFactory;
  41. }
  42. public function execute(Observer $observer)
  43. {
  44. /** @var \Magento\Catalog\Block\ShortcutButtons $shortcutButtons */
  45. $shortcutButtons = $observer->getEvent()->getContainer();
  46. if ($this->coreHelper->isPwaEnabled() && $this->coreHelper->isCurrentCurrencySupportedByAmazon()) {
  47. $params = [
  48. 'shortcutValidator' => $this->shortcutFactory->create($observer->getEvent()->getCheckoutSession()),
  49. ];
  50. $params['checkoutSession'] = $observer->getEvent()->getCheckoutSession();
  51. /** @var \Magento\Framework\View\Element\Template $shortcut */
  52. $shortcut = $shortcutButtons->getLayout()->createBlock(
  53. \Amazon\Payment\Block\Minicart\Button::class,
  54. '',
  55. $params
  56. );
  57. $shortcut->setIsInCatalogProduct(
  58. $observer->getEvent()->getIsCatalogProduct()
  59. )->setShowOrPosition(
  60. $observer->getEvent()->getOrPosition()
  61. );
  62. $shortcut->setIsCart(get_class($shortcutButtons) == \Magento\Checkout\Block\QuoteShortcutButtons::class);
  63. $shortcutButtons->addShortcut($shortcut);
  64. }
  65. }
  66. }