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

Display pickup location details in order confirmations #8727

Merged
merged 7 commits into from
Mar 17, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/Shipping/ShippingController.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@

use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\Tests\BlockTypes\Cart;
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
use Automattic\WooCommerce\Utilities\ArrayUtil;
@@ -59,12 +58,14 @@ function() {
add_action( 'admin_enqueue_scripts', [ $this, 'hydrate_client_settings' ] );
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_order_hide_shipping_address', array( $this, 'hide_shipping_address_for_local_pickup' ), 10 );
add_filter( 'woocommerce_customer_taxable_address', array( $this, 'filter_taxable_address' ) );
add_filter( 'woocommerce_shipping_packages', array( $this, 'filter_shipping_packages' ) );
add_filter( 'pre_update_option_woocommerce_pickup_location_settings', array( $this, 'flush_cache' ) );
add_filter( 'pre_update_option_pickup_location_pickup_locations', array( $this, 'flush_cache' ) );
add_filter( 'woocommerce_shipping_settings', array( $this, 'remove_shipping_settings' ) );
add_filter( 'wc_shipping_enabled', array( $this, 'force_shipping_enabled' ), 100, 1 );
add_filter( 'woocommerce_order_shipping_to_display', array( $this, 'show_local_pickup_details' ), 10, 2 );

// This is required to short circuit `show_shipping` from class-wc-cart.php - without it, that function
// returns based on the option's value in the DB and we can't override it any other way.
@@ -98,6 +99,39 @@ public function force_shipping_enabled( $enabled ) {
return $enabled;
}

/**
* Inject collection details onto the order received page.
*
* @param string $return Return value.
* @param \WC_Order $order Order object.
* @return string
*/
public function show_local_pickup_details( $return, $order ) {
// Confirm order is valid before proceeding further.
if ( ! $order instanceof \WC_Order ) {
return $return;
}

$shipping_method_ids = ArrayUtil::select( $order->get_shipping_methods(), 'get_method_id', ArrayUtil::SELECT_BY_OBJECT_METHOD );
$shipping_method_id = current( $shipping_method_ids );

// Ensure order used pickup location method, otherwise bail.
if ( 'pickup_location' !== $shipping_method_id ) {
return $return;
}

$shipping_method = current( $order->get_shipping_methods() );
$details = $shipping_method->get_meta( 'pickup_details' );
$location = $shipping_method->get_meta( 'pickup_location' );
$address = $shipping_method->get_meta( 'pickup_address' );

return sprintf(
// Translators: %s location name.
__( 'Pickup from <strong>%s</strong>:', 'woo-gutenberg-products-block' ),
$location
) . '<br/><address>' . str_replace( ',', ',<br/>', $address ) . '</address><br/>' . $details;
}

/**
* If the Checkout block Remove shipping settings from WC Core's admin panels that are now block settings.
*
@@ -341,6 +375,16 @@ public function register_local_pickup_method( $methods ) {
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.