|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © O2TI. All rights reserved. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bruno Elisei <[email protected]> |
|
6
|
|
|
* See COPYING.txt for license details. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace O2TI\FieldSortInCheckout\Block\Adminhtml\Form\Field\Address; |
|
12
|
|
|
|
|
13
|
|
|
use Magento\Customer\Model\Address; |
|
14
|
|
|
use Magento\Customer\Model\Customer; |
|
15
|
|
|
use Magento\Eav\Api\Data\AttributeInterface; |
|
16
|
|
|
use Magento\Framework\View\Element\Context; |
|
17
|
|
|
use Magento\Framework\View\Element\Html\Select; |
|
18
|
|
|
use Magento\Ui\Component\Form\AttributeMapper; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Address Fields To Sort - Create Field to Column. |
|
22
|
|
|
*/ |
|
23
|
|
|
class FieldColumn extends Select |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Address |
|
27
|
|
|
*/ |
|
28
|
|
|
private $address; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Customer |
|
32
|
|
|
*/ |
|
33
|
|
|
private $customer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var AttributeMapper |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $attributeMapper; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Context $context |
|
44
|
|
|
* @param Address $address |
|
45
|
|
|
* @param Customer $customer |
|
46
|
|
|
* @param AttributeMapper $attributeMapper |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
Context $context, |
|
51
|
|
|
Address $address, |
|
52
|
|
|
Customer $customer, |
|
53
|
|
|
AttributeMapper $attributeMapper, |
|
|
|
|
|
|
54
|
|
|
array $data = [] |
|
55
|
|
|
) { |
|
56
|
|
|
parent::__construct($context, $data); |
|
57
|
|
|
$this->address = $address; |
|
58
|
|
|
$this->customer = $customer; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set "name" for <select> element. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $value |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setInputName(string $value) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->setName($value); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set "id" for <select> element. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $value |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setInputId(string $value) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->setId($value); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render block HTML. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function _toHtml(): string |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$this->getOptions()) { |
|
93
|
|
|
$this->setOptions($this->getSourceOptions()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return parent::_toHtml(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get Source Options. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getSourceOptions(): array |
|
105
|
|
|
{ |
|
106
|
|
|
$customerAttributes = $this->getCustomerAttributes(); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($customerAttributes as $atribute => $val) { |
|
109
|
|
|
$attributesArrays[] = [ |
|
110
|
|
|
'label' => __($val['label']).' - '.$atribute, |
|
111
|
|
|
'value' => $atribute, |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $attributesArrays; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get customer attributes. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
private function getCustomerAttributes(): array |
|
124
|
|
|
{ |
|
125
|
|
|
$elements = []; |
|
126
|
|
|
|
|
127
|
|
|
/** @var AttributeInterface[] $attributes */ |
|
128
|
|
|
$attributesAddress = $this->address->getAttributes(); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($attributesAddress as $attribute) { |
|
131
|
|
|
$code = $attribute->getAttributeCode(); |
|
132
|
|
|
$inForms = $attribute->getUsedInForms(); |
|
133
|
|
|
$forms = []; |
|
134
|
|
|
foreach ($inForms as $value) { |
|
135
|
|
|
$forms[] = $value; |
|
136
|
|
|
} |
|
137
|
|
|
if (!in_array('customer_register_address', $forms)) { |
|
138
|
|
|
continue; |
|
139
|
|
|
} |
|
140
|
|
|
if ((int) $attribute->getData('is_visible') === 0) { |
|
141
|
|
|
continue; |
|
142
|
|
|
} |
|
143
|
|
|
$elements[$code]['label'] = $attribute->getData('frontend_label'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $elements; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.