33
44use Automattic \WooCommerce \Blocks \Assets \Api as AssetApi ;
55use Automattic \WooCommerce \Blocks \Assets \AssetDataRegistry ;
6+ use Automattic \WooCommerce \StoreApi \Utilities \LocalPickupUtils ;
67use 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 ;
0 commit comments