-
Notifications
You must be signed in to change notification settings - Fork 70
Add shipping options to Cart API responses (6271) #4306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stracker-phil
merged 15 commits into
dev/PCP-4891-agentic-commerce
from
dev/PCP-6271-shipping-options
Apr 29, 2026
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f41da6f
🧪 Add tests for planned ShippingOptionBuilder
stracker-phil 8cf5661
✨ Initial version of the ShippingOptionBuilder
stracker-phil 129cd88
👔 Inject new builder into cart factory
stracker-phil 9fb6a3d
🔀 Merge branch 'PCP-6085-remove-test-output'
stracker-phil 9cb8878
✅ Fix wrong namespace in test file
stracker-phil 69a41eb
🧪 Greatly extend the CartResponse tests
stracker-phil 5663e85
♻️ Consolidate all cart responses in one class
stracker-phil 903c531
✅ Adjust outdated test syntax after last commit
stracker-phil 11d4091
🔥 Delete old cart response classes
stracker-phil c8e3ca6
♻️ Update the cart response factory to new syntax
stracker-phil fc15571
🔀 Merge branch 'dev/develop'
stracker-phil 6b7cf63
🔥 Remove incorrect test
stracker-phil 6e13bb6
♻️ Simplify and document ShippingOptionsBuilder
stracker-phil bbf6794
♻️ Replace number_format with CartHelper method
stracker-phil ba741bf
🔀 Merge branch 'dev/PCP-4891-agentic-commerce'
stracker-phil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
modules/ppcp-store-sync/src/Helper/ShippingOptionsBuilder.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
| /** | ||
| * Builds the available_shipping_options array for cart API responses. | ||
| * | ||
| * @package WooCommerce\PayPalCommerce\StoreSync\Helper | ||
| */ | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace WooCommerce\PayPalCommerce\StoreSync\Helper; | ||
|
|
||
| use WC_Cart; | ||
|
|
||
| class ShippingOptionsBuilder { | ||
|
|
||
| /** | ||
| * Build shipping options from the WC cart state post calculate_totals(). | ||
| * | ||
| * Returns an empty array when no cart is available or no packages exist. | ||
| * Exactly one option in a non-empty result has is_selected = true. | ||
| * | ||
| * @param WC_Cart|null $wc_cart The WooCommerce cart. | ||
| * @return array | ||
| */ | ||
| public function build( ?WC_Cart $wc_cart ): array { | ||
| if ( null === $wc_cart ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $packages = WC()->shipping()->get_packages(); | ||
|
|
||
| if ( empty( $packages ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); | ||
| $chosen_id = ( is_array( $chosen_methods ) && ! empty( $chosen_methods ) ) | ||
| ? $chosen_methods[0] | ||
| : null; | ||
|
|
||
| $currency = get_woocommerce_currency(); | ||
| $options = array(); | ||
| $first_rate_id = null; | ||
|
|
||
| foreach ( $packages as $package ) { | ||
| if ( empty( $package['rates'] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ( $package['rates'] as $rate ) { | ||
| $rate_id = $rate->get_id(); | ||
|
|
||
| if ( null === $first_rate_id ) { | ||
| $first_rate_id = $rate_id; | ||
| } | ||
|
|
||
| $options[] = array( | ||
| 'id' => $rate_id, | ||
|
stracker-phil marked this conversation as resolved.
Outdated
|
||
| 'label' => $rate->get_label(), | ||
| 'amount' => number_format( (float) $rate->get_cost(), 2, '.', '' ), | ||
|
Narek13 marked this conversation as resolved.
Outdated
|
||
| 'currency' => $currency, | ||
| 'is_selected' => false, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if ( empty( $options ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $selected_id = $chosen_id ?? $first_rate_id; | ||
| foreach ( $options as &$option ) { | ||
| $option['is_selected'] = ( $option['id'] === $selected_id ); | ||
| } | ||
| unset( $option ); | ||
|
|
||
| return $options; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.