Reorder.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Helper;
  7. /**
  8. * Sales module base helper
  9. */
  10. class Reorder extends \Magento\Framework\App\Helper\AbstractHelper
  11. {
  12. const XML_PATH_SALES_REORDER_ALLOW = 'sales/reorder/allow';
  13. /**
  14. * @var \Magento\Customer\Model\Session
  15. */
  16. protected $customerSession;
  17. /**
  18. * @var \Magento\Sales\Api\OrderRepositoryInterface
  19. */
  20. protected $orderRepository;
  21. /**
  22. * @param \Magento\Framework\App\Helper\Context $context
  23. * @param \Magento\Customer\Model\Session $customerSession
  24. * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  25. */
  26. public function __construct(
  27. \Magento\Framework\App\Helper\Context $context,
  28. \Magento\Customer\Model\Session $customerSession,
  29. \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  30. ) {
  31. $this->orderRepository = $orderRepository;
  32. $this->customerSession = $customerSession;
  33. parent::__construct(
  34. $context
  35. );
  36. }
  37. /**
  38. * @return bool
  39. */
  40. public function isAllow()
  41. {
  42. return $this->isAllowed();
  43. }
  44. /**
  45. * Check if reorder is allowed for given store
  46. *
  47. * @param \Magento\Store\Model\Store|int|null $store
  48. * @return bool
  49. */
  50. public function isAllowed($store = null)
  51. {
  52. if ($this->scopeConfig->getValue(
  53. self::XML_PATH_SALES_REORDER_ALLOW,
  54. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  55. $store
  56. )) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Check is it possible to reorder
  63. *
  64. * @param int $orderId
  65. * @return bool
  66. */
  67. public function canReorder($orderId)
  68. {
  69. $order = $this->orderRepository->get($orderId);
  70. if (!$this->isAllowed($order->getStore())) {
  71. return false;
  72. }
  73. if ($this->customerSession->isLoggedIn()) {
  74. return $order->canReorder();
  75. } else {
  76. return true;
  77. }
  78. }
  79. }