tils::get_local_pickup_method_ids(), $shipping_method_ids ) ); } ); // Remove pickup location from rates arrays if not all packages can be picked up or support local pickup. if ( count( $valid_packages ) !== count( $packages ) ) { $packages = array_map( function ( $package ) { if ( ! is_array( $package['rates'] ) ) { $package['rates'] = array(); return $package; } $package['rates'] = array_filter( $package['rates'], function ( $rate ) { return ! in_array( $rate->get_method_id(), LocalPickupUtils::get_local_pickup_method_ids(), true ); } ); return $package; }, $packages ); } return $packages; } /** * Remove shipping (i.e. delivery, not local pickup) if "Hide shipping costs until an address is entered" is enabled, * and no address has been entered yet. * * Only applies to block checkout because pickup is chosen separately to shipping in that context. * * @param array $packages Array of shipping packages. * @return array */ public function remove_shipping_if_no_address( $packages ) { if ( 'shortcode' === WC()->cart->cart_context ) { return $packages; } $shipping_cost_requires_address = wc_string_to_bool( get_option( 'woocommerce_shipping_cost_requires_address', 'no' ) ); // Return early here for a small performance gain if we don't need to hide shipping costs until an address is entered. if ( ! $shipping_cost_requires_address ) { return $packages; } $customer = WC()->customer; if ( $customer instanceof WC_Customer && $customer->has_full_shipping_address() ) { return $packages; } return array_map( function ( $package ) { // Package rates is always an array due to a check in core. $package['rates'] = array_filter( $package['rates'], function ( $rate ) { return $rate instanceof WC_Shipping_Rate && in_array( $rate->get_method_id(), LocalPickupUtils::get_local_pickup_method_ids(), true ); } ); return $package; }, $packages ); } /** * Track local pickup settings changes via Store API * * @param bool $served Whether the request has already been served. * @param \WP_REST_Response $result The response object. * @param \WP_REST_Request $request The request object. * @return bool */ public function track_local_pickup( $served, $result, $request ) { if ( '/wp/v2/settings' !== $request->get_route() ) { return $served; } // Param name here comes from the show_in_rest['name'] value when registering the setting. if ( ! $request->get_param( 'pickup_location_settings' ) && ! $request->get_param( 'pickup_locations' ) ) { return $served; } $event_name = 'local_pickup_save_changes'; $settings = $request->get_param( 'pickup_location_settings' ); $locations = $request->get_param( 'pickup_locations' ); $data = array( 'local_pickup_enabled' => 'yes' === $settings['enabled'] ? true : false, 'title' => __( 'Pickup', 'woocommerce' ) === $settings['title'], 'price' => '' === $settings['cost'] ? true : false, 'cost' => '' === $settings['cost'] ? 0 : $settings['cost'], 'taxes' => $settings['tax_status'], 'total_pickup_locations' => count( $locations ), 'pickup_locations_enabled' => count( array_filter( $locations, function ( $location ) { return $location['enabled']; } ) ), ); WC_Tracks::record_event( $event_name, $data ); return $served; } /** * Check if legacy local pickup is activated in any of the shipping zones or in the Rest of the World zone. * * @since 8.8.0 * * @return bool */ public static function is_legacy_local_pickup_active() { $rest_of_the_world = \WC_Shipping_Zones::get_zone_by( 'zone_id', 0 ); $shipping_zones = \WC_Shipping_Zones::get_zones(); $rest_of_the_world_data = $rest_of_the_world->get_data(); $rest_of_the_world_data['shipping_methods'] = $rest_of_the_world->get_shipping_methods(); array_unshift( $shipping_zones, $rest_of_the_world_data ); foreach ( $shipping_zones as $zone ) { foreach ( $zone['shipping_methods'] as $method ) { if ( 'local_pickup' === $method->id && $method->is_enabled() ) { return true; } } } return false; } }