This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathShippingController.php
266 lines (250 loc) · 7.98 KB
/
ShippingController.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace Automattic\WooCommerce\Blocks\Shipping;
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* ShippingController class.
*
* @internal
*/
class ShippingController {
/**
* Instance of the asset API.
*
* @var AssetApi
*/
protected $asset_api;
/**
* Instance of the asset data registry.
*
* @var AssetDataRegistry
*/
protected $asset_data_registry;
/**
* Constructor.
*
* @param AssetApi $asset_api Instance of the asset API.
* @param AssetDataRegistry $asset_data_registry Instance of the asset data registry.
*/
public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) {
$this->asset_api = $asset_api;
$this->asset_data_registry = $asset_data_registry;
}
/**
* Initialization method.
*/
public function init() {
// @todo This should be moved inline for the settings page only.
$this->asset_data_registry->add(
'pickupLocationSettings',
get_option( 'woocommerce_pickup_location_settings', [] ),
true
);
$this->asset_data_registry->add(
'pickupLocations',
function() {
$locations = get_option( 'pickup_location_pickup_locations', [] );
$formatted = [];
foreach ( $locations as $location ) {
$formatted[] = [
'name' => $location['name'],
'address' => $location['address'],
'details' => $location['details'],
'enabled' => wc_string_to_bool( $location['enabled'] ),
];
}
return $formatted;
},
true
);
if ( is_admin() ) {
$this->asset_data_registry->add(
'countryStates',
function() {
return WC()->countries->get_states();
},
true
);
}
add_action( 'rest_api_init', [ $this, 'register_settings' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
add_action( 'woocommerce_load_shipping_methods', array( $this, 'register_local_pickup' ) );
add_filter( 'woocommerce_local_pickup_methods', array( $this, 'register_local_pickup_method' ) );
add_filter( 'woocommerce_customer_taxable_address', array( $this, 'filter_taxable_address' ) );
add_filter( 'woocommerce_shipping_packages', array( $this, 'filter_shipping_packages' ) );
}
/**
* Register Local Pickup settings for rest api.
*/
public function register_settings() {
register_setting(
'options',
'woocommerce_pickup_location_settings',
[
'type' => 'object',
'description' => 'WooCommerce Local Pickup Method Settings',
'default' => [],
'show_in_rest' => [
'name' => 'pickup_location_settings',
'schema' => [
'type' => 'object',
'properties' => array(
'enabled' => [
'description' => __( 'If enabled, this method will appear on the block based checkout.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'enum' => [ 'yes', 'no' ],
],
'title' => [
'description' => __( 'This controls the title which the user sees during checkout.', 'woo-gutenberg-products-block' ),
'type' => 'string',
],
'tax_status' => [
'description' => __( 'If a cost is defined, this controls if taxes are applied to that cost.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'enum' => [ 'taxable', 'none' ],
],
'cost' => [
'description' => __( 'Optional cost to charge for local pickup.', 'woo-gutenberg-products-block' ),
'type' => 'string',
],
),
],
],
]
);
register_setting(
'options',
'pickup_location_pickup_locations',
[
'type' => 'array',
'description' => 'WooCommerce Local Pickup Locations',
'default' => [],
'show_in_rest' => [
'name' => 'pickup_locations',
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => array(
'name' => [
'type' => 'string',
],
'address' => [
'type' => 'object',
'properties' => array(
'address_1' => [
'type' => 'string',
],
'city' => [
'type' => 'string',
],
'state' => [
'type' => 'string',
],
'postcode' => [
'type' => 'string',
],
'country' => [
'type' => 'string',
],
),
],
'details' => [
'type' => 'string',
],
'enabled' => [
'type' => 'boolean',
],
),
],
],
],
]
);
}
/**
* Load admin scripts.
*/
public function admin_scripts() {
$this->asset_api->register_script( 'wc-shipping-method-pickup-location', 'build/wc-shipping-method-pickup-location.js', [], true );
}
/**
* Registers the Local Pickup shipping method used by the Checkout Block.
*/
public function register_local_pickup() {
wc()->shipping->register_shipping_method( new PickupLocation() );
}
/**
* Declares the Pickup Location shipping method as a Local Pickup method for WooCommerce.
*
* @param array $methods Shipping method ids.
* @return array
*/
public function register_local_pickup_method( $methods ) {
$methods[] = 'pickup_location';
return $methods;
}
/**
* Filter the location used for taxes based on the chosen pickup location.
*
* @param array $address Location args.
* @return array
*/
public function filter_taxable_address( $address ) {
// We only need to select from the first package, since pickup_location only supports a single package.
$chosen_method = current( WC()->session->get( 'chosen_shipping_methods', array() ) ) ?? '';
$chosen_method_id = explode( ':', $chosen_method )[0];
$chosen_method_instance = explode( ':', $chosen_method )[1] ?? 0;
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
if ( $chosen_method_id && true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && 'pickup_location' === $chosen_method_id ) {
$pickup_locations = get_option( 'pickup_location_pickup_locations', [] );
$pickup_location = $pickup_locations[ $chosen_method_instance ] ?? [];
if ( isset( $pickup_location['address'], $pickup_location['address']['country'] ) && ! empty( $pickup_location['address']['country'] ) ) {
$address = array(
$pickup_locations[ $chosen_method_instance ]['address']['country'],
$pickup_locations[ $chosen_method_instance ]['address']['state'],
$pickup_locations[ $chosen_method_instance ]['address']['postcode'],
$pickup_locations[ $chosen_method_instance ]['address']['city'],
);
}
}
return $address;
}
/**
* Local Pickup requires all packages to support local pickup. This is because the entire order must be picked up
* so that all packages get the same tax rates applied during checkout.
*
* If a shipping package does not support local pickup (e.g. if disabled by an extension), this filters the option
* out for all packages. This will in turn disable the "pickup" toggle in Block Checkout.
*
* @param array $packages Array of shipping packages.
* @return array
*/
public function filter_shipping_packages( $packages ) {
// Check all packages for an instance of the pickup_location shipping method.
$valid_packages = array_filter(
$packages,
function( $package ) {
$shipping_method_ids = ArrayUtil::select( $package['rates'] ?? [], 'get_method_id', ArrayUtil::SELECT_BY_OBJECT_METHOD );
return in_array( 'pickup_location', $shipping_method_ids, true );
}
);
// Remove pickup location from rates arrays.
if ( count( $valid_packages ) !== count( $packages ) ) {
$packages = array_map(
function( $package ) {
$package['rates'] = array_filter(
$package['rates'],
function( $rate ) {
return 'pickup_location' !== $rate->get_method_id();
}
);
return $package;
},
$packages
);
}
return $packages;
}
}