-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarrier.php
213 lines (194 loc) · 6.27 KB
/
Carrier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Nolwennig\PickupInStore\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Config;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Magento\Quote\Model\Quote\Address\RateResult\Method;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Psr\Log\LoggerInterface;
/**
* Class Carrier In-Store Pickup shipping model
*/
class Carrier extends AbstractCarrier implements CarrierInterface
{
/**
* Carrier's code
*
* @var string
*/
protected $_code = 'storepickup';
/**
* Whether this carrier has fixed rates calculation
*
* @var bool
*/
protected $_isFixed = true;
/**
* @var ResultFactory
*/
protected $rateResultFactory;
/**
* @var MethodFactory
*/
protected $rateMethodFactory;
/**
* @param ScopeConfigInterface $scopeConfig
* @param ErrorFactory $rateErrorFactory
* @param LoggerInterface $logger
* @param ResultFactory $rateResultFactory
* @param MethodFactory $rateMethodFactory
* @param array $data
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
ErrorFactory $rateErrorFactory,
LoggerInterface $logger,
ResultFactory $rateResultFactory,
MethodFactory $rateMethodFactory,
array $data = []
) {
$this->rateResultFactory = $rateResultFactory;
$this->rateMethodFactory = $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* Generates list of allowed carrier`s shipping methods
* Displays on cart price rules page
*
* @return array
* @api
*/
public function getAllowedMethods()
{
return [$this->getCarrierCode() => __($this->getConfigData('name'))];
}
/**
* Collect and get rates for storefront
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param RateRequest $request
* @return DataObject|bool|null
* @api
*/
public function collectRates(RateRequest $request)
{
/**
* Make sure that Shipping method is enabled
*/
if (!$this->isActive()) {
return false;
}
/**
* Build Rate for each location
* Each Rate displayed as shipping method under Carrier(In-Store Pickup) on frontend
*/
$result = $this->rateResultFactory->create();
foreach ($this->getLocations() as $locationId => $location) {
$method = $this->buildRateForLocation($locationId, $location);
$result->append($method);
}
return $result;
}
/**
* Build Rate based on location data
*
* @param string $locationId Shipping method(location) identifier
* @param array $location Location info
* @return Method
*/
protected function buildRateForLocation($locationId, array $location)
{
$rateResultMethod = $this->rateMethodFactory->create();
/**
* Set carrier's method data
*/
$rateResultMethod->setData('carrier', $this->getCarrierCode());
$rateResultMethod->setData('carrier_title', $this->getConfigData('title'));
/**
* Displayed as shipping method under Carrier(In-Store Pickup)
*/
$methodTitle = sprintf(
'%s : %s (%s)',
$rateResultMethod->getData('carrier_title'),
$location['city'],
$location['postcode']
);
$rateResultMethod->setData('method_title', $methodTitle);
$rateResultMethod->setData('estimate_shipping', __LINE__);
$rateResultMethod->setData('shipping_infobulle', __LINE__);
$rateResultMethod->setData('method', $locationId);
// $rateResultMethod->setPrice(10);
// $rateResultMethod->setData('cost', 10);
return $rateResultMethod;
}
/**
* Get locations info
*
* @return array
*/
protected function getLocations()
{
$locations = [];
$configData = $this->getConfigData('store_locations');
if (is_string($configData) && !empty($configData)) {
$locations = unserialize($configData);
}
$shippingOrigin = $this->getShippingOrigin();
$result = [];
$i=0;
foreach ($locations as $location) {
/**
* Use Location Title => 'location_title_{$i}' as identifier of shipping method
*/
$locationId = strtolower(str_replace(' ', '_', $location['title'])) . $i++;
$result[$locationId] = [
'title' => $location['title'],
'street' => $location['street'],
'phone' => $location['phone'],
'message' => $location['message']
];
$result[$locationId] = array_merge($result[$locationId], $shippingOrigin);
}
return $result;
}
/**
* Get configured Store Shipping Origin
*
* @return array
*/
protected function getShippingOrigin()
{
/**
* Get Shipping origin data from store scope config
* Displays data on storefront
*/
return [
'country_id' => $this->_scopeConfig->getValue(
Config::XML_PATH_ORIGIN_COUNTRY_ID,
ScopeInterface::SCOPE_STORE,
$this->getData('store')
),
'region_id' => $this->_scopeConfig->getValue(
Config::XML_PATH_ORIGIN_REGION_ID,
ScopeInterface::SCOPE_STORE,
$this->getData('store')
),
'postcode' => $this->_scopeConfig->getValue(
Config::XML_PATH_ORIGIN_POSTCODE,
ScopeInterface::SCOPE_STORE,
$this->getData('store')
),
'city' => $this->_scopeConfig->getValue(
Config::XML_PATH_ORIGIN_CITY,
ScopeInterface::SCOPE_STORE,
$this->getData('store')
)
];
}
}