ValueProvider.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\Rule\Metadata;
  7. use Magento\SalesRule\Model\Rule;
  8. use Magento\Store\Model\System\Store;
  9. use Magento\Customer\Api\GroupRepositoryInterface;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Framework\Convert\DataObject;
  12. /**
  13. * Metadata provider for sales rule edit form.
  14. */
  15. class ValueProvider
  16. {
  17. /**
  18. * @var Store
  19. */
  20. protected $store;
  21. /**
  22. * @var GroupRepositoryInterface
  23. */
  24. protected $groupRepository;
  25. /**
  26. * @var SearchCriteriaBuilder
  27. */
  28. protected $searchCriteriaBuilder;
  29. /**
  30. * @var DataObject
  31. */
  32. protected $objectConverter;
  33. /**
  34. * @var \Magento\SalesRule\Model\RuleFactory
  35. */
  36. protected $salesRuleFactory;
  37. /**
  38. * Initialize dependencies.
  39. *
  40. * @param Store $store
  41. * @param GroupRepositoryInterface $groupRepository
  42. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  43. * @param DataObject $objectConverter
  44. * @param \Magento\SalesRule\Model\RuleFactory $salesRuleFactory
  45. */
  46. public function __construct(
  47. Store $store,
  48. GroupRepositoryInterface $groupRepository,
  49. SearchCriteriaBuilder $searchCriteriaBuilder,
  50. DataObject $objectConverter,
  51. \Magento\SalesRule\Model\RuleFactory $salesRuleFactory
  52. ) {
  53. $this->store = $store;
  54. $this->groupRepository = $groupRepository;
  55. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  56. $this->objectConverter = $objectConverter;
  57. $this->salesRuleFactory = $salesRuleFactory;
  58. }
  59. /**
  60. * Get metadata for sales rule form. It will be merged with form UI component declaration.
  61. *
  62. * @param Rule $rule
  63. * @return array
  64. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  65. */
  66. public function getMetadataValues(\Magento\SalesRule\Model\Rule $rule)
  67. {
  68. $customerGroups = $this->groupRepository->getList($this->searchCriteriaBuilder->create())->getItems();
  69. $applyOptions = [
  70. ['label' => __('Percent of product price discount'), 'value' => Rule::BY_PERCENT_ACTION],
  71. ['label' => __('Fixed amount discount'), 'value' => Rule::BY_FIXED_ACTION],
  72. ['label' => __('Fixed amount discount for whole cart'), 'value' => Rule::CART_FIXED_ACTION],
  73. ['label' => __('Buy X get Y free (discount amount is Y)'), 'value' => Rule::BUY_X_GET_Y_ACTION]
  74. ];
  75. $couponTypesOptions = [];
  76. $couponTypes = $this->salesRuleFactory->create()->getCouponTypes();
  77. foreach ($couponTypes as $key => $couponType) {
  78. $couponTypesOptions[] = [
  79. 'label' => $couponType,
  80. 'value' => $key,
  81. ];
  82. }
  83. $labels = $rule->getStoreLabels();
  84. return [
  85. 'rule_information' => [
  86. 'children' => [
  87. 'website_ids' => [
  88. 'arguments' => [
  89. 'data' => [
  90. 'config' => [
  91. 'options' => $this->store->getWebsiteValuesForForm(),
  92. ],
  93. ],
  94. ],
  95. ],
  96. 'is_active' => [
  97. 'arguments' => [
  98. 'data' => [
  99. 'config' => [
  100. 'options' => [
  101. ['label' => __('Active'), 'value' => '1'],
  102. ['label' => __('Inactive'), 'value' => '0']
  103. ],
  104. ],
  105. ],
  106. ],
  107. ],
  108. 'customer_group_ids' => [
  109. 'arguments' => [
  110. 'data' => [
  111. 'config' => [
  112. 'options' => $this->objectConverter->toOptionArray($customerGroups, 'id', 'code'),
  113. ],
  114. ],
  115. ],
  116. ],
  117. 'coupon_type' => [
  118. 'arguments' => [
  119. 'data' => [
  120. 'config' => [
  121. 'options' => $couponTypesOptions,
  122. ],
  123. ],
  124. ],
  125. ],
  126. 'is_rss' => [
  127. 'arguments' => [
  128. 'data' => [
  129. 'config' => [
  130. 'options' => [
  131. ['label' => __('Yes'), 'value' => '1'],
  132. ['label' => __('No'), 'value' => '0']
  133. ],
  134. ],
  135. ],
  136. ],
  137. ],
  138. ]
  139. ],
  140. 'actions' => [
  141. 'children' => [
  142. 'simple_action' => [
  143. 'arguments' => [
  144. 'data' => [
  145. 'config' => [
  146. 'options' => $applyOptions
  147. ],
  148. ]
  149. ]
  150. ],
  151. 'discount_amount' => [
  152. 'arguments' => [
  153. 'data' => [
  154. 'config' => [
  155. 'value' => '0',
  156. ],
  157. ],
  158. ],
  159. ],
  160. 'discount_qty' => [
  161. 'arguments' => [
  162. 'data' => [
  163. 'config' => [
  164. 'value' => '0',
  165. ],
  166. ],
  167. ],
  168. ],
  169. 'apply_to_shipping' => [
  170. 'arguments' => [
  171. 'data' => [
  172. 'config' => [
  173. 'options' => [
  174. ['label' => __('Yes'), 'value' => '1'],
  175. ['label' => __('No'), 'value' => '0']
  176. ]
  177. ],
  178. ],
  179. ],
  180. ],
  181. 'stop_rules_processing' => [
  182. 'arguments' => [
  183. 'data' => [
  184. 'config' => [
  185. 'options' => [
  186. ['label' => __('Yes'), 'value' => '1'],
  187. ['label' => __('No'), 'value' => '0'],
  188. ],
  189. ],
  190. ]
  191. ]
  192. ],
  193. ]
  194. ],
  195. 'labels' => [
  196. 'children' => [
  197. 'store_labels[0]' => [
  198. 'arguments' => [
  199. 'data' => [
  200. 'config' => [
  201. 'value' => isset($labels[0]) ? $labels[0] : '',
  202. ],
  203. ]
  204. ]
  205. ]
  206. ]
  207. ],
  208. ];
  209. }
  210. }