'city' => array(
'type' => 'string',
),
'state' => array(
'type' => 'string',
),
'postcode' => array(
'type' => 'string',
),
'country' => array(
'type' => 'string',
),
),
),
'details' => array(
'type' => 'string',
),
'enabled' => array(
'type' => 'boolean',
),
),
),
),
),
)
);
}
/**
* Hydrate client settings
*/
public function hydrate_client_settings() {
$locations = get_option( 'pickup_location_pickup_locations', array() );
$formatted_pickup_locations = array();
foreach ( $locations as $location ) {
$formatted_pickup_locations[] = array(
'name' => $location['name'],
'address' => $location['address'],
'details' => $location['details'],
'enabled' => wc_string_to_bool( $location['enabled'] ),
);
}
$has_legacy_pickup = false;
// Get all shipping zones.
$shipping_zones = \WC_Shipping_Zones::get_zones( 'admin' );
$international_shipping_zone = new \WC_Shipping_Zone( 0 );
// Loop through each shipping zone.
foreach ( $shipping_zones as $shipping_zone ) {
// Get all registered rates for this shipping zone.
$shipping_methods = $shipping_zone['shipping_methods'];
// Loop through each registered rate.
foreach ( $shipping_methods as $shipping_method ) {
if ( 'local_pickup' === $shipping_method->id && 'yes' === $shipping_method->enabled ) {
$has_legacy_pickup = true;
break 2;
}
}
}
foreach ( $international_shipping_zone->get_shipping_methods( true ) as $shipping_method ) {
if ( 'local_pickup' === $shipping_method->id ) {
$has_legacy_pickup = true;
break;
}
}
$settings = array(
'pickupLocationSettings' => LocalPickupUtils::get_local_pickup_settings(),
'pickupLocations' => $formatted_pickup_locations,
'readonlySettings' => array(
'hasLegacyPickup' => $has_legacy_pickup,
'storeCountry' => WC()->countries->get_base_country(),
'storeState' => WC()->countries->get_base_state(),
),
);
wp_add_inline_script(
'wc-shipping-method-pickup-location',
sprintf(
'var hydratedScreenSettings = %s;',
wp_json_encode( $settings )
),
'before'
);
}
/**
* Load admin scripts.
*/
public function admin_scripts() {
$this->asset_api->register_script( 'wc-shipping-method-pickup-location', 'assets/client/blocks/wc-shipping-method-pickup-location.js', array(), true );
}
/**
* Registers the Local Pickup shipping method used by the Checkout Block.
*/
public function register_local_pickup() {
if ( CartCheckoutUtils::is_checkout_block_default() ) {
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;
}
/**
* Hides the shipping address on the order confirmation page when local pickup is selected.
*
* @param array $pickup_methods Method ids.
* @return array
*/
public function hide_shipping_address_for_local_pickup( $pickup_methods ) {
return array_merge( $pickup_methods, LocalPickupUtils::get_local_pickup_method_ids() );
}
/**
* Everytime we save or update local pickup settings, we flush the shipping
* transient group.
*
* @param array $settings The setting array we're saving.
* @return array $settings The setting array we're saving.
*/
public function flush_cache( $settings ) {
\WC_Cache_Helper::get_transient_version( 'shipping', true );
return $settings;
}
/**
* 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 ) {
if ( null === WC()->session ) {
return $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 ) && in_array( $chosen_method_id, LocalPickupUtils::get_local_pickup_method_ids(), true ) ) {
$pickup_locations = get_option( 'pickup_location_pickup_locations', array() );
$pickup_location = $pickup_locations[ $chosen_method_instance ] ?? array();
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 a collectable shipping method.
$valid_packages = array_filter(
$packages,
function ( $package ) {
$shipping_method_ids = ArrayUtil::select( $package['rates'] ?? array(), 'get_method_id', ArrayUtil::SELECT_BY_OBJECT_METHOD );
return ! empty( array_intersect( LocalPickupUtils::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;
}
/**
* Checks whether the address is "full" in the sense that it contains all required fields to calculate shipping rates.
*
* @return bool Whether the customer has a full shipping address (address_1, city, state, postcode, country).
* Only required fields are checked.
*/
public function has_full_shipping_address() {
$customer = WC()->customer;
if ( ! $customer instanceof WC_Customer ) {
return false;
}
// These are the important fields required to get the shipping rates.
$shipping_address = array(
'city' => $customer->get_shipping_city(),
'state' => $customer->get_shipping_state(),
'postcode' => $customer->get_shipping_postcode(),
'country' => $customer->get_shipping_country(),
);
$address_fields = WC()->countries->get_country_locale();
$locale_key = ! empty( $shipping_address['country'] ) && array_key_exists( $shipping_address['country'], $address_fields ) ? $shipping_address['country'] : 'default';
$default_locale = $address_fields['default'];
$country_locale = $address_fields[ $locale_key ] ?? array();
/**
* Checks all shipping address fields against the country's locale settings.
*
* If there's a `required` setting for the field in the country-specific locale, that setting is used, otherwise
* the default locale's setting is used. If the default locale doesn't have a setting either, the field is
* considered optional and therefore valid, even if empty.
*/
foreach ( $shipping_address as $key => $value ) {
// Skip further checks if the field has a value. From this point on $value is empty.
if ( ! empty( $value ) ) {
continue;
}
$locale_to_check = isset( $country_locale[ $key ]['required'] ) ? $country_locale : $default_locale;
// If the locale requires the field return false.
if ( isset( $locale_to_check[ $key ]['required'] ) && true === wc_string_to_bool( $locale_to_check[ $key ]['required'] ) ) {
return false;
}
}
return true;
}
/**
* 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.
*
* @param array $packages Array of shipping packages.
* @return array
*/
public function remove_shipping_if_no_address( $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;
}
$has_full_address = $this->has_full_shipping_address();
if ( $has_full_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;
}
}