Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Address.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  protected $_directoryCountry;
14 
19 
24 
29 
38  public function __construct(
39  \Magento\Rule\Model\Condition\Context $context,
40  \Magento\Directory\Model\Config\Source\Country $directoryCountry,
41  \Magento\Directory\Model\Config\Source\Allregion $directoryAllregion,
42  \Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods,
43  \Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods,
44  array $data = []
45  ) {
46  parent::__construct($context, $data);
47  $this->_directoryCountry = $directoryCountry;
48  $this->_directoryAllregion = $directoryAllregion;
49  $this->_shippingAllmethods = $shippingAllmethods;
50  $this->_paymentAllmethods = $paymentAllmethods;
51  }
52 
58  public function loadAttributeOptions()
59  {
60  $attributes = [
61  'base_subtotal' => __('Subtotal'),
62  'total_qty' => __('Total Items Quantity'),
63  'weight' => __('Total Weight'),
64  'shipping_method' => __('Shipping Method'),
65  'postcode' => __('Shipping Postcode'),
66  'region' => __('Shipping Region'),
67  'region_id' => __('Shipping State/Province'),
68  'country_id' => __('Shipping Country'),
69  ];
70 
71  $this->setAttributeOption($attributes);
72 
73  return $this;
74  }
75 
81  public function getAttributeElement()
82  {
83  $element = parent::getAttributeElement();
84  $element->setShowAsText(true);
85  return $element;
86  }
87 
93  public function getInputType()
94  {
95  switch ($this->getAttribute()) {
96  case 'base_subtotal':
97  case 'weight':
98  case 'total_qty':
99  return 'numeric';
100 
101  case 'shipping_method':
102  case 'payment_method':
103  case 'country_id':
104  case 'region_id':
105  return 'select';
106  }
107  return 'string';
108  }
109 
115  public function getValueElementType()
116  {
117  switch ($this->getAttribute()) {
118  case 'shipping_method':
119  case 'payment_method':
120  case 'country_id':
121  case 'region_id':
122  return 'select';
123  }
124  return 'text';
125  }
126 
132  public function getValueSelectOptions()
133  {
134  if (!$this->hasData('value_select_options')) {
135  switch ($this->getAttribute()) {
136  case 'country_id':
137  $options = $this->_directoryCountry->toOptionArray();
138  break;
139 
140  case 'region_id':
141  $options = $this->_directoryAllregion->toOptionArray();
142  break;
143 
144  case 'shipping_method':
145  $options = $this->_shippingAllmethods->toOptionArray();
146  break;
147 
148  case 'payment_method':
149  $options = $this->_paymentAllmethods->toOptionArray();
150  break;
151 
152  default:
153  $options = [];
154  }
155  $this->setData('value_select_options', $options);
156  }
157  return $this->getData('value_select_options');
158  }
159 
166  public function validate(\Magento\Framework\Model\AbstractModel $model)
167  {
168  $address = $model;
169  if (!$address instanceof \Magento\Quote\Model\Quote\Address) {
170  if ($model->getQuote()->isVirtual()) {
171  $address = $model->getQuote()->getBillingAddress();
172  } else {
173  $address = $model->getQuote()->getShippingAddress();
174  }
175  }
176 
177  if ('payment_method' == $this->getAttribute() && !$address->hasPaymentMethod()) {
178  $address->setPaymentMethod($model->getQuote()->getPayment()->getMethod());
179  }
180 
181  return parent::validate($address);
182  }
183 }
validate(\Magento\Framework\Model\AbstractModel $model)
Definition: Address.php:166
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$address
Definition: customer.php:38
$attributes
Definition: matrix.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72
__construct(\Magento\Rule\Model\Condition\Context $context, \Magento\Directory\Model\Config\Source\Country $directoryCountry, \Magento\Directory\Model\Config\Source\Allregion $directoryAllregion, \Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods, \Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods, array $data=[])
Definition: Address.php:38
$element
Definition: element.phtml:12