Skip to content
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

Add ShippingOption override to cart #1316

Merged
merged 7 commits into from
Oct 30, 2023
Merged

Add ShippingOption override to cart #1316

merged 7 commits into from
Oct 30, 2023

Conversation

alecritson
Copy link
Collaborator

This PR looks to add the ability to carts for developers to override a shipping option for a cart in a single request, useful if you want to estimate what the total cost a cart will have with a certain shipping option selection, to present to the end user.

Usage

You can get an estimated shipping option for the cart like so:

$cart->getEstimatedShipping([
    'postcode' => '123456',
    'state' => 'Essex',
    'country' => Country::first(),
]);

This will fetch the cheapest shipping option from the manifest, but by default will not set the shipping override, so pipelines will function as they currently do. In order for the pipelines to take the override into account you just pass the additional setOverride parameter:

$cart->getEstimatedShipping([
    'postcode' => '123456',
    'state' => 'Essex',
    'country' => Country::first(),
], setOverride: true);

Now when you call calculate() on a cart, the override will be taken into account, bypassing any shipping address logic you may have in place.

This is a "once" off effect so, say you have another request that calls calculate() without setting the meta above, the pipeline will not take it into account.

If you use the CartSession facade, this can remember the meta for you in the session, making subsequent calls easier:

CartSession::estimateShippingUsing([
    'postcode' => '123456',
    'state' => 'Essex',
    'country' => Country::first(),
]);

You may not need to provide all arguments, but may affect how shipping options are resolved and which are added to the manifest.

Once set though, you can pass the optional estimateShipping parameter when getting the current cart.

This will use the shipping override in the pipelines, based on the

CartSession::current(estimateShipping: true);

Behind the scenes, we're just using the getEstimatedShipping on the cart as we fetch it and setting the override before the calculate() method is called.

@vercel
Copy link

vercel bot commented Oct 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 30, 2023 9:50am

@glennjacobs
Copy link
Contributor

Nice. I think it just needs some docs adding.

@glennjacobs
Copy link
Contributor

Can we change this to an H2 in the docs so the shipping header shows on the right?

### Adding shipping/billing address

@@ -16,6 +16,7 @@ public function __construct(
public TaxClass $taxClass,
public $taxReference = null,
public $option = null,
public bool $collection = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I just spotted this was added. I presume it's for orders that are being collected and not delivered.

Can we add the usage to the docs? Maybe also at the same time give an example of how you can manually override the shipping option directly on the cart too?

glennjacobs
glennjacobs previously approved these changes Oct 30, 2023
@alecritson alecritson merged commit 12cb50a into main Oct 30, 2023
12 checks passed
@alecritson alecritson deleted the feat/shipping-override branch October 30, 2023 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants