Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 6e913c6

Browse files
oprdinhtungdu
andauthored
Fix more instances of hardcoded pickup_location string (#8542)
* Move get_local_pickup_method_ids to LocalPickupUtils file This is because we need it to be accessible by other classes. * Remove pickup_location strings & get all collectable methods dynamically --------- Co-authored-by: Tung Du <[email protected]>
1 parent 7f85c21 commit 6e913c6

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

src/BlockTypes/Checkout.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Automattic\WooCommerce\Blocks\BlockTypes;
33

44
use Automattic\WooCommerce\Blocks\Package;
5+
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
56

67
/**
78
* Checkout class.
@@ -277,7 +278,7 @@ function() {
277278
$formatted_shipping_methods = array_reduce(
278279
$shipping_methods,
279280
function( $acc, $method ) {
280-
if ( 'pickup_location' === $method->id ) {
281+
if ( in_array( $method->id, LocalPickupUtils::get_local_pickup_method_ids(), true ) ) {
281282
return $acc;
282283
}
283284
if ( $method->supports( 'settings' ) ) {

src/Shipping/ShippingController.php

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
55
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
6+
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
67
use Automattic\WooCommerce\Utilities\ArrayUtil;
78

89
/**
@@ -49,7 +50,7 @@ function() {
4950
true
5051
);
5152
}
52-
$this->asset_data_registry->add( 'collectableMethodIds', array( $this, 'get_local_pickup_method_ids' ), true );
53+
$this->asset_data_registry->add( 'collectableMethodIds', array( 'Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils', 'get_local_pickup_method_ids' ), true );
5354
add_action( 'rest_api_init', [ $this, 'register_settings' ] );
5455
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
5556
add_action( 'admin_enqueue_scripts', [ $this, 'hydrate_client_settings' ] );
@@ -61,32 +62,6 @@ function() {
6162
add_filter( 'pre_update_option_pickup_location_pickup_locations', array( $this, 'flush_cache' ) );
6263
}
6364

64-
/**
65-
* Gets a list of payment method ids that support the 'local-pickup' feature.
66-
*
67-
* @return string[] List of payment method ids that support the 'local-pickup' feature.
68-
*/
69-
public function get_local_pickup_method_ids() {
70-
$all_methods_supporting_local_pickup = array_reduce(
71-
WC()->shipping()->get_shipping_methods(),
72-
function( $methods, $method ) {
73-
if ( $method->supports( 'local-pickup' ) ) {
74-
$methods[] = $method->id;
75-
}
76-
return $methods;
77-
},
78-
array()
79-
);
80-
81-
// We use array_values because this will be used in JS, so we don't need the (numerical) keys.
82-
return array_values(
83-
// This array_unique is necessary because WC()->shipping()->get_shipping_methods() can return duplicates.
84-
array_unique(
85-
$all_methods_supporting_local_pickup
86-
)
87-
);
88-
}
89-
9065
/**
9166
* Register Local Pickup settings for rest api.
9267
*/
@@ -293,7 +268,7 @@ public function filter_taxable_address( $address ) {
293268
$chosen_method_instance = explode( ':', $chosen_method )[1] ?? 0;
294269

295270
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
296-
if ( $chosen_method_id && true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && 'pickup_location' === $chosen_method_id ) {
271+
if ( $chosen_method_id && true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && in_array( $chosen_method_id, LocalPickupUtils::get_local_pickup_method_ids(), true ) ) {
297272
$pickup_locations = get_option( 'pickup_location_pickup_locations', [] );
298273
$pickup_location = $pickup_locations[ $chosen_method_instance ] ?? [];
299274

@@ -321,12 +296,12 @@ public function filter_taxable_address( $address ) {
321296
* @return array
322297
*/
323298
public function filter_shipping_packages( $packages ) {
324-
// Check all packages for an instance of the pickup_location shipping method.
299+
// Check all packages for an instance of a collectable shipping method.
325300
$valid_packages = array_filter(
326301
$packages,
327302
function( $package ) {
328303
$shipping_method_ids = ArrayUtil::select( $package['rates'] ?? [], 'get_method_id', ArrayUtil::SELECT_BY_OBJECT_METHOD );
329-
return in_array( 'pickup_location', $shipping_method_ids, true );
304+
return ! empty( array_intersect( LocalPickupUtils::get_local_pickup_method_ids(), $shipping_method_ids ) );
330305
}
331306
);
332307

@@ -337,7 +312,7 @@ function( $package ) {
337312
$package['rates'] = array_filter(
338313
$package['rates'],
339314
function( $rate ) {
340-
return 'pickup_location' !== $rate->get_method_id();
315+
return ! in_array( $rate->get_method_id(), LocalPickupUtils::get_local_pickup_method_ids(), true );
341316
}
342317
);
343318
return $package;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Automattic\WooCommerce\StoreApi\Utilities;
3+
4+
/**
5+
* Util class for local pickup related functionality, this contains methods that need to be accessed from places besides
6+
* the ShippingController, i.e. the OrderController.
7+
*/
8+
class LocalPickupUtils {
9+
/**
10+
* Gets a list of payment method ids that support the 'local-pickup' feature.
11+
*
12+
* @return string[] List of payment method ids that support the 'local-pickup' feature.
13+
*/
14+
public static function get_local_pickup_method_ids() {
15+
$all_methods_supporting_local_pickup = array_reduce(
16+
WC()->shipping()->get_shipping_methods(),
17+
function( $methods, $method ) {
18+
if ( $method->supports( 'local-pickup' ) ) {
19+
$methods[] = $method->id;
20+
}
21+
return $methods;
22+
},
23+
array()
24+
);
25+
26+
// We use array_values because this will be used in JS, so we don't need the (numerical) keys.
27+
return array_values(
28+
// This array_unique is necessary because WC()->shipping()->get_shipping_methods() can return duplicates.
29+
array_unique(
30+
$all_methods_supporting_local_pickup
31+
)
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)