Address.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\Rule\Condition;
  7. /**
  8. * Address rule condition data model.
  9. */
  10. class Address extends \Magento\Rule\Model\Condition\AbstractCondition
  11. {
  12. /**
  13. * @var \Magento\Directory\Model\Config\Source\Country
  14. */
  15. protected $_directoryCountry;
  16. /**
  17. * @var \Magento\Directory\Model\Config\Source\Allregion
  18. */
  19. protected $_directoryAllregion;
  20. /**
  21. * @var \Magento\Shipping\Model\Config\Source\Allmethods
  22. */
  23. protected $_shippingAllmethods;
  24. /**
  25. * @var \Magento\Payment\Model\Config\Source\Allmethods
  26. */
  27. protected $_paymentAllmethods;
  28. /**
  29. * @param \Magento\Rule\Model\Condition\Context $context
  30. * @param \Magento\Directory\Model\Config\Source\Country $directoryCountry
  31. * @param \Magento\Directory\Model\Config\Source\Allregion $directoryAllregion
  32. * @param \Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods
  33. * @param \Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Rule\Model\Condition\Context $context,
  38. \Magento\Directory\Model\Config\Source\Country $directoryCountry,
  39. \Magento\Directory\Model\Config\Source\Allregion $directoryAllregion,
  40. \Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods,
  41. \Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods,
  42. array $data = []
  43. ) {
  44. parent::__construct($context, $data);
  45. $this->_directoryCountry = $directoryCountry;
  46. $this->_directoryAllregion = $directoryAllregion;
  47. $this->_shippingAllmethods = $shippingAllmethods;
  48. $this->_paymentAllmethods = $paymentAllmethods;
  49. }
  50. /**
  51. * Load attribute options
  52. *
  53. * @return $this
  54. */
  55. public function loadAttributeOptions()
  56. {
  57. $attributes = [
  58. 'base_subtotal' => __('Subtotal'),
  59. 'total_qty' => __('Total Items Quantity'),
  60. 'weight' => __('Total Weight'),
  61. 'payment_method' => __('Payment Method'),
  62. 'shipping_method' => __('Shipping Method'),
  63. 'postcode' => __('Shipping Postcode'),
  64. 'region' => __('Shipping Region'),
  65. 'region_id' => __('Shipping State/Province'),
  66. 'country_id' => __('Shipping Country'),
  67. ];
  68. $this->setAttributeOption($attributes);
  69. return $this;
  70. }
  71. /**
  72. * Get attribute element
  73. *
  74. * @return $this
  75. */
  76. public function getAttributeElement()
  77. {
  78. $element = parent::getAttributeElement();
  79. $element->setShowAsText(true);
  80. return $element;
  81. }
  82. /**
  83. * Get input type
  84. *
  85. * @return string
  86. */
  87. public function getInputType()
  88. {
  89. switch ($this->getAttribute()) {
  90. case 'base_subtotal':
  91. case 'weight':
  92. case 'total_qty':
  93. return 'numeric';
  94. case 'shipping_method':
  95. case 'payment_method':
  96. case 'country_id':
  97. case 'region_id':
  98. return 'select';
  99. }
  100. return 'string';
  101. }
  102. /**
  103. * Get value element type
  104. *
  105. * @return string
  106. */
  107. public function getValueElementType()
  108. {
  109. switch ($this->getAttribute()) {
  110. case 'shipping_method':
  111. case 'payment_method':
  112. case 'country_id':
  113. case 'region_id':
  114. return 'select';
  115. }
  116. return 'text';
  117. }
  118. /**
  119. * Get value select options
  120. *
  121. * @return array|mixed
  122. */
  123. public function getValueSelectOptions()
  124. {
  125. if (!$this->hasData('value_select_options')) {
  126. switch ($this->getAttribute()) {
  127. case 'country_id':
  128. $options = $this->_directoryCountry->toOptionArray();
  129. break;
  130. case 'region_id':
  131. $options = $this->_directoryAllregion->toOptionArray();
  132. break;
  133. case 'shipping_method':
  134. $options = $this->_shippingAllmethods->toOptionArray();
  135. break;
  136. case 'payment_method':
  137. $options = $this->_paymentAllmethods->toOptionArray();
  138. break;
  139. default:
  140. $options = [];
  141. }
  142. $this->setData('value_select_options', $options);
  143. }
  144. return $this->getData('value_select_options');
  145. }
  146. /**
  147. * Validate Address Rule Condition
  148. *
  149. * @param \Magento\Framework\Model\AbstractModel $model
  150. * @return bool
  151. */
  152. public function validate(\Magento\Framework\Model\AbstractModel $model)
  153. {
  154. $address = $model;
  155. if (!$address instanceof \Magento\Quote\Model\Quote\Address) {
  156. if ($model->getQuote()->isVirtual()) {
  157. $address = $model->getQuote()->getBillingAddress();
  158. } else {
  159. $address = $model->getQuote()->getShippingAddress();
  160. }
  161. }
  162. if ('payment_method' == $this->getAttribute() && !$address->hasPaymentMethod()) {
  163. $address->setPaymentMethod($model->getQuote()->getPayment()->getMethod());
  164. }
  165. return parent::validate($address);
  166. }
  167. }