Coupon.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Helper;
  7. /**
  8. * Helper for coupon codes creating and managing
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Coupon extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15. /**
  16. * Constants which defines all possible coupon codes formats
  17. */
  18. const COUPON_FORMAT_ALPHANUMERIC = 'alphanum';
  19. const COUPON_FORMAT_ALPHABETICAL = 'alpha';
  20. const COUPON_FORMAT_NUMERIC = 'num';
  21. /**
  22. * Defines type of Coupon
  23. */
  24. const COUPON_TYPE_SPECIFIC_AUTOGENERATED = 1;
  25. /**
  26. * XML paths to coupon codes generation options
  27. */
  28. const XML_PATH_SALES_RULE_COUPON_LENGTH = 'promo/auto_generated_coupon_codes/length';
  29. const XML_PATH_SALES_RULE_COUPON_FORMAT = 'promo/auto_generated_coupon_codes/format';
  30. const XML_PATH_SALES_RULE_COUPON_PREFIX = 'promo/auto_generated_coupon_codes/prefix';
  31. const XML_PATH_SALES_RULE_COUPON_SUFFIX = 'promo/auto_generated_coupon_codes/suffix';
  32. const XML_PATH_SALES_RULE_COUPON_DASH_INTERVAL = 'promo/auto_generated_coupon_codes/dash';
  33. /**
  34. * @var array
  35. */
  36. protected $_couponParameters;
  37. /**
  38. * @param \Magento\Framework\App\Helper\Context $context
  39. * @param array $couponParameters
  40. */
  41. public function __construct(
  42. \Magento\Framework\App\Helper\Context $context,
  43. array $couponParameters
  44. ) {
  45. $this->_couponParameters = $couponParameters;
  46. parent::__construct($context);
  47. }
  48. /**
  49. * Get all possible coupon codes formats
  50. *
  51. * @return array
  52. */
  53. public function getFormatsList()
  54. {
  55. return [
  56. self::COUPON_FORMAT_ALPHANUMERIC => __('Alphanumeric'),
  57. self::COUPON_FORMAT_ALPHABETICAL => __('Alphabetical'),
  58. self::COUPON_FORMAT_NUMERIC => __('Numeric')
  59. ];
  60. }
  61. /**
  62. * Get default coupon code length
  63. *
  64. * @return int
  65. */
  66. public function getDefaultLength()
  67. {
  68. return (int)$this->scopeConfig->getValue(
  69. self::XML_PATH_SALES_RULE_COUPON_LENGTH,
  70. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  71. );
  72. }
  73. /**
  74. * Get default coupon code format
  75. *
  76. * @return int
  77. */
  78. public function getDefaultFormat()
  79. {
  80. return $this->scopeConfig->getValue(
  81. self::XML_PATH_SALES_RULE_COUPON_FORMAT,
  82. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  83. );
  84. }
  85. /**
  86. * Get default coupon code prefix
  87. *
  88. * @return string
  89. */
  90. public function getDefaultPrefix()
  91. {
  92. return $this->scopeConfig->getValue(
  93. self::XML_PATH_SALES_RULE_COUPON_PREFIX,
  94. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  95. );
  96. }
  97. /**
  98. * Get default coupon code suffix
  99. *
  100. * @return string
  101. */
  102. public function getDefaultSuffix()
  103. {
  104. return $this->scopeConfig->getValue(
  105. self::XML_PATH_SALES_RULE_COUPON_SUFFIX,
  106. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  107. );
  108. }
  109. /**
  110. * Get dashes occurrences frequency in coupon code
  111. *
  112. * @return int
  113. */
  114. public function getDefaultDashInterval()
  115. {
  116. return (int)$this->scopeConfig->getValue(
  117. self::XML_PATH_SALES_RULE_COUPON_DASH_INTERVAL,
  118. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  119. );
  120. }
  121. /**
  122. * Get Coupon's alphabet as array of chars
  123. *
  124. * @param string $format
  125. * @return array|bool
  126. */
  127. public function getCharset($format)
  128. {
  129. return str_split($this->_couponParameters['charset'][$format]);
  130. }
  131. /**
  132. * Retrieve Separator
  133. *
  134. * @return string
  135. */
  136. public function getCodeSeparator()
  137. {
  138. return $this->_couponParameters['separator'];
  139. }
  140. }