I'd like to run the shipping rate's calculate_quote() method on clicking "Checkout" in the cart, so the default (perhaps only) shipping rate is calculated and applied to the order at that stage.
In my circumstance, the shipping rates are calculated based on the contents of the basket, and are fixed in that the shopper can't select different options. So the rate can always be calculated just from the cart contents, and the shipping needs to be re-calculated when the cart contents are changed.
Current problem:
1) User selects items to buy, they go to Checkout. No shipping price is shown, OK.
2) User continues to "Review order" page. Shipping is calculated, and price is shown. OK.
3) User returns to basket, and changes the items in the basket. OK.
4) User returns to Checkout page. Shipping price line item is still as calculated back in step 2, and is not updated to take new basket contents into account. ERROR!!
5) User continues to "Review order" page. Shipping is now calculated, and correct price is shown. OK.
Would it be possible to change the calculation trigger from the current "Submit Checkout form" to be activated by a Rule? Then the moment that shipping costs are calculated could be changed as needed.
Comments
Comment #1
mariomc commentedI'm subscribing to this.
The best solution seems to be via a rule that behaves similarly to "Commerce Tax - calculate taxes for the line item".
Comment #2
rszrama commentedI think we may need to consider the following solution:
Comment #3
fonant commentedAgree with rszrama, although for my purposes there isn't any need to refresh the first checkout page as I only have one shipping method. If we can find a way to calculate shipping as a rule that can be evaluated when the checkout page is displayed, that would be excellent.
I think perhaps the most flexible method would be to turn "Calculate shipping" into an action that any rule can fire?
And perhaps add a "Shipping method changed" event too?
Commerce seems to be using Rules a great deal, and doing so certainly gives plenty of flexibility :)
Comment #4
rszrama commentedI'm fixing this in the 2.x branch by implementing a default Rule that deletes shipping when an order's status is updated from non-"Shopping cart" to "Shopping cart." This will capture any time products are added to the cart / the cart form is submitted / the customer cancels out of checkout. When they come back to checkout, shipping will be recalculated and reapplied to the order on form submission. There is no automatic re-calculation baked into the default rule, but it should be a trivial action to implement if that's what you prefer.
Comment #5
adpo commentedIt sounds like a good idea.
PS> If we could use universal module called Extra Fee, it will be great,
Comment #6
paul.linney commentedCan someone provide more info on how to achieve the auto re-calc on view of the cart page - using Shipping 2.x? As with Fonant, I have only one shipping method without any options.
I have tried triggering the 'Add a rate for a shipping service to an order' action rule on the existing 'Delete shipping line items on shopping cart updates' rule, after it has deleted the shipping line items and tried a new rule to call the same action after Adding/Removing an item from the cart.
My Rules knowledge may be lacking for this, but any help would be appreciated.
Comment #7
muschpusch commentedThis isn't the nicest way to do it but i'm short on time and it could easily be used write an rules action
[edit]some cleanup [/edit]
Comment #8
finex commentedWhy not moving the "Shipping service" pane on the checkout page ( /admin/commerce/config/checkout/form ) ?
Comment #9
muschpusch commentedSorry don't use the hook above use:
@Finex: that wouldn't work on a one page checkout
Comment #10
agoradesign commentedThank you very much muschpusch, you're definitely my personal hero of the day. I was trying to solve this problem the whole afternoon, until I found your post here with only a few simple lines of code :))
Comment #11
rszrama commentedIf I'm not mistaken, the code to calculate shipping on the initial pageload was committed a long time ago. Based on #4, it looks like I was leaving the door open to recalculate a shipping rate instead of deleting all shipping line items on cart refresh like we do now. To do this, you'd simply edit the default rule that deletes shipping line items and replace that action with the one added in #1452322: Rule to Auto-add shipping service. It does look like there's some follow-up work to do in that issue, but this should be sufficient to close this issue out.
Comment #13
guypaddock commentedAlso see:
https://www.drupal.org/project/commerce_shipping_on_review