Button.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Block;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Framework\View\Element\Template\Context;
  9. use Magento\InstantPurchase\Model\Config;
  10. /**
  11. * Configuration for JavaScript instant purchase button component.
  12. *
  13. * @api
  14. * @since 100.2.0
  15. */
  16. class Button extends Template
  17. {
  18. /**
  19. * @var Config
  20. */
  21. private $instantPurchaseConfig;
  22. /**
  23. * Button constructor.
  24. * @param Context $context
  25. * @param Config $instantPurchaseConfig
  26. * @param array $data
  27. */
  28. public function __construct(
  29. Context $context,
  30. Config $instantPurchaseConfig,
  31. array $data = []
  32. ) {
  33. parent::__construct($context, $data);
  34. $this->instantPurchaseConfig = $instantPurchaseConfig;
  35. }
  36. /**
  37. * Checks if button enabled.
  38. *
  39. * @return bool
  40. * @since 100.2.0
  41. */
  42. public function isEnabled(): bool
  43. {
  44. return $this->instantPurchaseConfig->isModuleEnabled($this->getCurrentStoreId());
  45. }
  46. /**
  47. * @inheritdoc
  48. * @since 100.2.0
  49. */
  50. public function getJsLayout(): string
  51. {
  52. $buttonText = $this->instantPurchaseConfig->getButtonText($this->getCurrentStoreId());
  53. $purchaseUrl = $this->getUrl('instantpurchase/button/placeOrder', ['_secure' => true]);
  54. // String data does not require escaping here and handled on transport level and on client side
  55. $this->jsLayout['components']['instant-purchase']['config']['buttonText'] = $buttonText;
  56. $this->jsLayout['components']['instant-purchase']['config']['purchaseUrl'] = $purchaseUrl;
  57. return parent::getJsLayout();
  58. }
  59. /**
  60. * Returns active store view identifier.
  61. *
  62. * @return int
  63. */
  64. private function getCurrentStoreId(): int
  65. {
  66. return $this->_storeManager->getStore()->getId();
  67. }
  68. }