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

Allow third party methods to appear in local pickup area #8256

Merged
merged 28 commits into from
Feb 3, 2023

Conversation

opr
Copy link
Contributor

@opr opr commented Jan 20, 2023

This PR will allow third party shipping methods that support 'collectible' to be displayed under local pickup.

To achieve this, I created a method in ShippingController.php to get all registered methods that declare collectible support, then add their IDs to the collectibleMethodIds key in the asset data registry.

This can be retrieved on the client by getSetting< string[] >( 'collectibleMethodIds', [] ).

When deciding which methods to show in the checkout-pickup-options block it now checks to see if the method name is in this collectibleMethodIds array or if the method id is pickup_location.

It also removes methods, following the same rules as above, from being displayed in the "regular" shipping options area.

I also modified assets/js/base/context/hooks/shipping/types.ts to set packageId as optional, this type definition did not correctly reflect the implementation.

This PR also removes hardcoded references to pickup_location when it is used to determine whether a local pickup method is in use, and instead uses the collectibleMethodIds site setting.

Fixes #8012

Testing

Automated Tests

  • Changes in this PR are covered by Automated Tests.
    • Unit tests
    • E2E tests

Internal developer testing

  1. Add the following code somewhere on your site, it will register a test shipping method called "Amazon locker".
Expand for example shipping method
function amazon_locker_shipping_init() {
	class Amazon_Locker_Shipping_Method extends WC_Shipping_Method {

		/**
		 * Min amount to be valid.
		 *
		 * @var integer
		 */
		public $min_amount = 0;

		/**
		 * Requires option.
		 *
		 * @var string
		 */
		public $requires = '';

		/**
		 * Constructor.
		 *
		 * @param int $instance_id Shipping method instance.
		 */
		public function __construct( $instance_id = 0 ) {
			$this->id                 = 'amazon_locker_shipping';
			$this->instance_id        = absint( $instance_id );
                        $this->title              = 'Amazon locker';
			$this->method_title       = __( 'Amazon locker', 'woocommerce' );
			$this->method_description = __( 'Get your order shipped to an amazon locker.', 'woocommerce' );
			$this->supports           = array(
				'instance-settings',
				'instance-settings-modal',
				'local-pickup',
			);

			$this->init();
		}

		/**
		 * Initialize Amazon Locker shipping.
		 */
		public function init() {
		}

		/**
		 * See if Amazon locker shipping is available based on the package and cart.
		 *
		 * @param array $package Shipping package.
		 * @return bool
		 */
		public function is_available( $package ) {
			return true;
		}

		/**
		 * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
		 *
		 * @param array $package Shipping package.
		 * @uses WC_Shipping_Method::add_rate()
		 */
		public function calculate_shipping( $package = array() ) {
			$this->add_rate(
				array(
					'label'   => $this->title,
					'cost'    => 0,
					'taxes'   => false,
					'package' => $package,
				)
			);
		}
	}
}

add_action( 'woocommerce_shipping_init', 'amazon_locker_shipping_init' ); // use this hook to initialize your new custom method

function add_amazon_locker_shipping( $methods ) {
	$methods['amazon_locker_shipping'] = 'Amazon_Locker_Shipping_Method';

	return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_amazon_locker_shipping' );
  1. Enable this method for one of your shipping zones. Note, the settings UI doesn't work properly for this method but that is OK.
  2. Enable the "normal" local pickup too, add a location.
  3. Add some items to your cart, go to the Cart page, enter an address using the shipping calculator. Ensure you see the local pickup option and the amazon locker option.
  4. Go to the Checkout and, without selecting local pickup, scroll down to the shipping methods section, you should not see amazon locker there.
  5. Choose local pickup, and ensure the amazon locker option appears.

User Facing Testing

  1. Enable local pickup on your site.
  2. Add items to your cart and go to the Cart block.
  3. Add an address using the Shipping Calculator. Ensure you see the local pickup options you added.
  4. Go to the Checkout page and, without selecting local pickup, scroll down to the shipping options area. Ensure you do not see local pickup there.
  5. Select local pickup and ensure the pickup location is shown.
  6. Check out successfully.
  • Do not include in the Testing Notes

WooCommerce Visibility

  • WooCommerce Core
  • Feature plugin
  • Experimental

Performance Impact

Changelog

Allow third party shipping methods to declare compatibility with WC Blocks local pickup.

@opr opr added status: needs review type: enhancement The issue is a request for an enhancement. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. labels Jan 20, 2023
@opr opr self-assigned this Jan 20, 2023
@opr opr requested review from senadir and mikejolley and removed request for senadir January 20, 2023 18:44
@woocommercebot woocommercebot requested a review from a team January 20, 2023 18:44
@github-actions
Copy link
Contributor

github-actions bot commented Jan 20, 2023

The release ZIP for this PR is accessible via:

https://wcblocks.wpcomstaging.com/wp-content/uploads/woocommerce-gutenberg-products-block-8256.zip

Script Dependencies Report

The compare-assets action has detected some changed script dependencies between this branch and trunk. Please review and confirm the following are correct before merging.

Script Handle Added Removed
reviews-frontend.js react, wc-settings, wp-a11y, wp-api-fetch, wp-compose, wp-element, wp-i18n, wp-is-shallow-equal, wp-polyfill ⚠️
active-filters-frontend.js lodash, react, wc-blocks-data-store, wc-price-format, wc-settings, wp-data, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url ⚠️
all-products-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
attribute-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
cart-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-plugins, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
checkout-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-plugins, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
filter-wrapper-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-price-format, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
mini-cart-frontend.js wc-settings, wp-polyfill ⚠️
price-filter-frontend.js lodash, react, wc-blocks-data-store, wc-price-format, wc-settings, wp-data, wp-element, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-url ⚠️
rating-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
single-product-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
stock-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
mini-cart-component-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-price-format, wc-settings, wp-a11y, wp-autop, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️

This comment was automatically generated by the ./github/compare-assets action.

TypeScript Errors Report

  • Files with errors: 500
  • Total errors: 2340

⚠️ ⚠️ This PR introduces new TS errors on 1 files:

assets/js/base/context/hooks/use-checkout-address.ts

comments-aggregator

@github-actions
Copy link
Contributor

github-actions bot commented Jan 20, 2023

Size Change: +293 B (0%)

Total Size: 1.1 MB

Filename Size Change
build/active-filters-wrapper-frontend.js 6 kB +1 B (0%)
build/all-products-frontend.js 11.7 kB -6 B (0%)
build/all-products.js 33.7 kB -4 B (0%)
build/attribute-filter-wrapper-frontend.js 7.67 kB +3 B (0%)
build/blocks-checkout.js 41 kB -5 B (0%)
build/cart-blocks/cart-cross-sells-products-frontend.js 9.69 kB +3 B (0%)
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.08 kB +1 B (0%)
build/cart-blocks/cart-express-payment-frontend.js 722 B +2 B (0%)
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.37 kB -5 B (0%)
build/cart-blocks/cart-order-summary-frontend.js 1.24 kB -1 B (0%)
build/cart-blocks/filled-cart-frontend.js 654 B -2 B (0%)
build/cart-blocks/order-summary-coupon-form-frontend.js 1.63 kB -1 B (0%)
build/cart-blocks/order-summary-heading-frontend.js 455 B -1 B (0%)
build/cart-blocks/order-summary-shipping-frontend.js 14.8 kB -56 B (0%)
build/cart-frontend.js 28.8 kB +116 B (0%)
build/cart.js 47.8 kB +104 B (0%)
build/checkout-blocks/actions-frontend.js 1.86 kB +3 B (0%)
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 3.92 kB +2 B (0%)
build/checkout-blocks/billing-address-frontend.js 1.16 kB +1 B (0%)
build/checkout-blocks/express-payment-frontend.js 1.13 kB +1 B (0%)
build/checkout-blocks/order-note-frontend.js 1.14 kB +2 B (0%)
build/checkout-blocks/order-summary-shipping-frontend.js 14.9 kB -51 B (0%)
build/checkout-blocks/payment-frontend.js 8.33 kB -1 B (0%)
build/checkout-blocks/pickup-options-frontend.js 2.8 kB -5 B (0%)
build/checkout-blocks/shipping-address-frontend.js 1.12 kB +2 B (0%)
build/checkout-blocks/shipping-method-frontend.js 2.27 kB -4 B (0%)
build/checkout-blocks/shipping-methods-frontend.js 4.78 kB -51 B (-1%)
build/checkout-blocks/terms-frontend.js 1.56 kB -1 B (0%)
build/checkout-frontend.js 30.3 kB +106 B (0%)
build/checkout.js 43.5 kB +69 B (0%)
build/featured-category.js 13.1 kB -2 B (0%)
build/featured-product.js 13.4 kB -2 B (0%)
build/filter-wrapper-frontend.js 14.1 kB +5 B (0%)
build/mini-cart-component-frontend.js 27.9 kB -4 B (0%)
build/mini-cart-contents-block/footer-frontend.js 2.82 kB +1 B (0%)
build/mini-cart-contents.js 17 kB -1 B (0%)
build/mini-cart-frontend.js 2 kB -1 B (0%)
build/price-filter-frontend.js 13.9 kB +2 B (0%)
build/product-add-to-cart-frontend.js 6.72 kB +2 B (0%)
build/product-button-frontend.js 2.19 kB +1 B (0%)
build/product-category-list-frontend.js 1.19 kB +6 B (+1%)
build/product-price-frontend.js 2.29 kB +5 B (0%)
build/product-rating-frontend.js 1.62 kB +2 B (0%)
build/product-sale-badge-frontend.js 1.42 kB +1 B (0%)
build/product-stock-indicator-frontend.js 1.32 kB +2 B (0%)
build/product-summary-frontend.js 1.58 kB +1 B (0%)
build/product-tag-list-frontend.js 1.18 kB +1 B (0%)
build/rating-filter-frontend.js 21.4 kB +1 B (0%)
build/rating-filter-wrapper-frontend.js 6.19 kB -2 B (0%)
build/rating-filter.js 7.37 kB +3 B (0%)
build/reviews-frontend.js 7.14 kB -1 B (0%)
build/single-product-frontend.js 17.8 kB -10 B (0%)
build/single-product.js 9.97 kB -1 B (0%)
build/stock-filter.js 8.09 kB +2 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary-shipping--checkout-blocks--18f9376a-frontend.js 19.4 kB +4 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 7.53 kB +2 B (0%)
build/vendors--checkout-blocks/shipping-method-frontend.js 12 kB +3 B (0%)
build/vendors--checkout-blocks/shipping-methods-frontend.js 9.48 kB -1 B (0%)
build/wc-blocks-data.js 22 kB +53 B (0%)
build/wc-blocks-vendors.js 64.3 kB -1 B (0%)
ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 7.98 kB
build/active-filters.js 7.29 kB
build/all-reviews.js 7.67 kB
build/attribute-filter-frontend.js 22.9 kB
build/attribute-filter.js 12.3 kB
build/breadcrumbs.js 2.04 kB
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-items-frontend.js 299 B
build/cart-blocks/cart-line-items-frontend.js 1.07 kB
build/cart-blocks/cart-totals-frontend.js 321 B
build/cart-blocks/empty-cart-frontend.js 345 B
build/cart-blocks/order-summary-discount-frontend.js 2.13 kB
build/cart-blocks/order-summary-fee-frontend.js 274 B
build/cart-blocks/order-summary-subtotal-frontend.js 274 B
build/cart-blocks/order-summary-taxes-frontend.js 435 B
build/cart-blocks/proceed-to-checkout-frontend.js 1.24 kB
build/catalog-sorting.js 1.69 kB
build/checkout-blocks/contact-information-frontend.js 2.05 kB
build/checkout-blocks/fields-frontend.js 344 B
build/checkout-blocks/order-summary-cart-items-frontend.js 3.68 kB
build/checkout-blocks/order-summary-coupon-form-frontend.js 1.79 kB
build/checkout-blocks/order-summary-discount-frontend.js 2.3 kB
build/checkout-blocks/order-summary-fee-frontend.js 277 B
build/checkout-blocks/order-summary-frontend.js 1.25 kB
build/checkout-blocks/order-summary-subtotal-frontend.js 275 B
build/checkout-blocks/order-summary-taxes-frontend.js 435 B
build/checkout-blocks/totals-frontend.js 324 B
build/customer-account.js 3.08 kB
build/filter-wrapper.js 2.4 kB
build/general-style-rtl.css 1.31 kB
build/general-style.css 1.31 kB
build/handpicked-products.js 7.24 kB
build/legacy-template.js 2.87 kB
build/mini-cart-contents-block/empty-cart-frontend.js 366 B
build/mini-cart-contents-block/filled-cart-frontend.js 268 B
build/mini-cart-contents-block/items-frontend.js 237 B
build/mini-cart-contents-block/products-table-frontend.js 590 B
build/mini-cart-contents-block/shopping-button-frontend.js 313 B
build/mini-cart-contents-block/title-frontend.js 367 B
build/mini-cart.js 4.29 kB
build/price-filter-wrapper-frontend.js 6.99 kB
build/price-filter.js 8.35 kB
build/price-format.js 1.19 kB
build/product-add-to-cart--product-button--product-category-list--product-image--product-price--product-r--a0326d00.js 228 B
build/product-add-to-cart--product-button--product-image--product-rating--product-title.js 151 B
build/product-add-to-cart.js 8.61 kB
build/product-best-sellers.js 7.59 kB
build/product-button--product-category-list--product-image--product-price--product-rating--product-sale-b--e17c7c01.js 494 B
build/product-button--product-image--product-price--product-rating--product-sale-badge--product-title.js 258 B
build/product-button.js 3.99 kB
build/product-categories.js 2.36 kB
build/product-category-list.js 503 B
build/product-category.js 8.58 kB
build/product-image-frontend.js 2.19 kB
build/product-image.js 4.09 kB
build/product-new.js 7.58 kB
build/product-on-sale.js 7.91 kB
build/product-price.js 1.58 kB
build/product-query.js 5.9 kB
build/product-rating.js 920 B
build/product-results-count.js 1.66 kB
build/product-sale-badge.js 816 B
build/product-search.js 2.6 kB
build/product-sku-frontend.js 629 B
build/product-sku.js 378 B
build/product-stock-indicator.js 646 B
build/product-summary.js 920 B
build/product-tag-list.js 497 B
build/product-tag.js 8.07 kB
build/product-title-frontend.js 1.62 kB
build/product-title.js 3.46 kB
build/product-top-rated.js 7.82 kB
build/products-by-attribute.js 8.52 kB
build/reviews-by-category.js 11.2 kB
build/reviews-by-product.js 12.3 kB
build/stock-filter-frontend.js 21.1 kB
build/stock-filter-wrapper-frontend.js 5.85 kB
build/store-notices.js 1.65 kB
build/vendors--attribute-filter-wrapper--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary--82e4ed06-frontend.js 6.86 kB
build/vendors--attribute-filter-wrapper--rating-filter-wrapper--stock-filter-wrapper-frontend.js 7.7 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/cart-line-items--cart-blocks/cart-order--3c5fe802-frontend.js 5.26 kB
build/vendors--cart-blocks/cart-line-items--checkout-blocks/order-summary-cart-items--mini-cart-contents---233ab542-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/billing-address--checkout-blocks/order--5b8feb0b-frontend.js 4.83 kB
build/wc-blocks-editor-style-rtl.css 5.47 kB
build/wc-blocks-editor-style.css 5.47 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 933 B
build/wc-blocks-registry.js 3.16 kB
build/wc-blocks-shared-context.js 1.52 kB
build/wc-blocks-shared-hocs.js 1.73 kB
build/wc-blocks-style-rtl.css 25.7 kB
build/wc-blocks-style.css 25.7 kB
build/wc-blocks-vendors-style-rtl.css 1.96 kB
build/wc-blocks-vendors-style.css 1.96 kB
build/wc-blocks.js 2.63 kB
build/wc-payment-method-bacs.js 816 B
build/wc-payment-method-cheque.js 811 B
build/wc-payment-method-cod.js 909 B
build/wc-payment-method-paypal.js 837 B
build/wc-settings.js 2.6 kB
build/wc-shipping-method-pickup-location.js 29.7 kB
build/wp-directives-runtime.js 2.4 kB
build/wp-directives-vendors.js 7.89 kB

compressed-size-action

src/Shipping/ShippingController.php Outdated Show resolved Hide resolved
src/Shipping/ShippingController.php Outdated Show resolved Hide resolved
Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great, the test extension worked as intended. I've made a few suggestions for shared utils/naming inline. They are not blocking but I think the supports variable could use something else. I'll leave it with you.

array_filter(
WC()->shipping()->get_shipping_methods(),
function( $method ) {
return $method->supports( 'collectible' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to suggest we make this wording/variable slightly clearer by making it:

return $method->supports( 'collection' );

or

return $method->supports( 'local_pickup' );

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I will update it.


export interface minMaxPrices {
min: CartShippingPackageShippingRate | undefined;
max: CartShippingPackageShippingRate | undefined;
}

const collectibleMethodIds = getSetting< string[] >(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing wrong here, however, we could probably move this settings call to a new shared util which returns a function called isRateCollectable or similar. This can be consumed here and in assets/js/blocks/checkout/inner-blocks/checkout-pickup-options-block/block.tsx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this method accepted either a string or array of strings, you could also replace areRatesCollectible with this helper. The code that splits rate.split( ':' )[ 0 ] method ids would remain in place. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See changes in assets/js/base/utils/shipping-rates.ts, this just checks a rate, or rates, and doesn't take care of splitting

);
// `pickup_location` will always be collectible. Adding it here means we can avoid hard coding it elsewhere on
// the client.
$collectible_method_ids[] = 'pickup_location';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would the code above not return pickup_location?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @return string[] List of payment method ids that support the 'local_pickup' feature.
*/
public function get_local_pickup_method_ids() {
$methods_supporting_local_pickup = array_unique(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to go back to doing it this way as WC()->shipping()->get_shipping_methods() returns like this:

image

For some reason it can include the same method multiple times, so stripping out duplicates and only outputting the method ID seems most sensible.

Is there a cleaner way of doing this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:

		$methods_supporting_local_pickup = array_reduce(
			WC()->shipping()->get_shipping_methods(),
			function( $methods, $method ) {
				if ( $method->supports( 'local_pickup' ) ) {
					$methods[] = $method->id;
				}
				return $methods;
			},
			array()
		);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that looks better, but I still need to array_unique it, given WC()->shipping()->get_shipping_methods() can return the same one multiple times. I'll implement.

@opr opr requested a review from mikejolley February 2, 2023 14:17
Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still testing well 👍🏻 Let's ship it.

@opr opr merged commit c2e4cc0 into trunk Feb 3, 2023
@opr opr deleted the add/local-pickup-methods branch February 3, 2023 16:00
Copy link
Member

@senadir senadir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is a great PR, I left some small notes that we might address later.

) => void;
// Only true when ALL packages support local pickup. If true, we can show the collection/delivery toggle
isCollectable: boolean;
// True when a rate is currently being selected and persisted to the server.
isSelectingRate: boolean;

hasSelectedLocalPickup: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

@@ -118,7 +119,7 @@ const Block = (): JSX.Element | null => {

// Get pickup locations from the first shipping package.
const pickupLocations = ( shippingRates[ 0 ]?.shipping_rates || [] ).filter(
( { method_id: methodId } ) => methodId === 'pickup_location'
isPackageRateCollectable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPackageRateCollectable and hasCollectableRate seem to handle similar logic, why can't we use just one?

@@ -26,6 +26,7 @@ public function init() {
$this->title = $this->get_option( 'title' );
$this->tax_status = $this->get_option( 'tax_status' );
$this->cost = $this->get_option( 'cost' );
$this->supports = [ 'local-pickup' ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented, we currently don't have a dev documentation page for Local Pickup, but once we do, we should include this. Also this PR needs a dev note for this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support third party shipping rates in local pickup
3 participants