I've a simple shop with two possible shipping rates, whereas one can only appear at a given time. I'd like to hide the "shipping service" pane and automatically apply the shipping rate, for which it would be nice to have an action.

Attached patch adds that action.

Related note: The wording of the action "Add a rate for a shipping service to an order" is a bit confusing to me, as it just makes the rate available. Maybe using "make available for the order" or "enable for the order" would be more clear here.

Comments

fago’s picture

Status: Active » Needs review
fago’s picture

StatusFileSize
new989 bytes

FYI, I'm using this with a rule like the one attached.

googletorp’s picture

Status: Needs review » Fixed

Thanks for the patch fago, it looks good and I committed it.

Regarding the name of the Rule, I can understand why it's a bit confusing. I'm guessing Ryan named it this way because what the action actually does, is to add the shipping services to the order object. This is also how the rates are added as options. I'll try to see if we can find a way to make it more clear - thanks for the suggestions.

dready2011’s picture

Hey Fago,

this is exactly what i am looking for. But I cannot figure out yet how to use it.
What I've done so far:
1. Installed flat rate module. Created 2 flat rate shipping services:
- Free shipping for orders over the threshold of 40 Euro
- Standard Shipping for orders below the threshold with standard value of 3.8 Euro
2. I imported the rule you posted.

Now i'm not sure how to proceed. Do I have to change the actions of these flat rates servuces? I tried to change the actions from "Add a rate for a shipping service to an order" to "apply shipping rate to order". But the shipping service pane does still show up.

Could you describe the steps that have to made in order to automatically set the shipping rate and skip the shipping service pane? I am not really sure i understand what the rule does that you made.

Also: i would like to always show the shipping fee in the shopping cart block... any idea how to do this?

Thanks!

dready2011’s picture

I figured it out, finally. I am new to the commerce module, and didn't realize that I had to disable the shipping step under admin/commerce/config/checkout.
After changing the shipping rule to react to the event 'After updating an existing commerce order' everything works as I wanted.
To add the shipping cost to the Cart-block, I removed a filter from the view.

lavisrap’s picture

Thanks fago!

I was searching just for this functionality and it works fine in my shop: patch & rule.

Michael

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

felixSchl’s picture

Thanks for sharing, works nicely!

Edit: I noticed that problems do remain if you step back from the final checkout page to make adjustments (let's say to the shipping address), it will not update the resulting shipping charge.

E.g. I have two zones, national and international, when I click back and switch from national to international, the national shipping fees still persist until I manually update the cart.

mrpeanut’s picture

I must be missing something. I applied the patch and imported the rule. The flat rate rule that comes with the module is disabled.

Auto-add shipping service rule reacts After updating an existing commerce order. Yet the flat rate doesn't get applied.

Do I have to keep the default flat rate rule, or change the rule provided in #2?

mrpeanut’s picture

Status: Closed (fixed) » Active

And now I'm getting this error:

Fatal error: Cannot redeclare commerce_shipping_rate_apply() (previously declared in /home/sites/all/modules/commerce_shipping/commerce_shipping.rules.inc:214) in /home/sites/all/modules/commerce_shipping/commerce_shipping.rules.inc on line 275

I'm assuming it's related. I haven't made any other changes.

[edit] Doh, didn't realize it was committed and I'm on dev. Replacing commerce_shipping.rules.inc fixed that error. Rule still isn't working, though.

googletorp’s picture

Status: Active » Fixed

So is working then?

mrpeanut’s picture

I think I'm trying to do something different. I'd like a delivery fee of $5 to be charged to every order and for that to show up in the cart. I got it to show in the cart from #5.

I disabled the included flat rate rule and changed the rule from #2 to react "After adding a product to the cart" and also "After updating an existing commerce order."

I also changed data comparison (commerce order status) from Checkout: Review to Shopping cart.

However, when adding a product to the cart, it still doesn't automatically add the delivery fee.

[edit] I just removed all the conditions from that rule and it now works. That may break something in the future, but for now, I'm all set. Thanks!

arun_ms’s picture

Am getting the following error,

Integrity check for the imported configuratoin failed. Error message: Unknown action commerce_shipping_method_collect_rates..

when i import the above rule.

What should i do?

mrpeanut’s picture

@arun_ms — Do you have Commerce Shipping and Commerce Flat Rate installed, plus a flat rate service set up?

arun_ms’s picture

@MrPeanut - I installed Commerce Shipping and Commerce Shipping Flat Rate

I am newbie to drupal , i dont know about flat rate service set up.

When i tried to install Commerce Flat Rate it shows me error.

Thanks

mrpeanut’s picture

I believe that for this to work, you need to use Commerce Shipping (I'm using dev) and Commerce Flat Rate. If you're getting an error, post that in the issue queue for Commerce Flat Rate. The instructions are then on that module's project page for how to set up a flat rate service.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

diwant’s picture

Awesome! Thanks all, this is so money.

I actually removed the check for if a current shipping thing exists because otherwise I was able to go and fill in a location with cheaper shipping, click 'Go Back' and change the location and not have the shipping update to the newer, correct, option. It seems to work fine even with my change.

Crouchie’s picture

Hello dready2011,
how exactly did you add the shipping cost to the cart (#5)? In the Shopping cart form view is only this filter: (Position) Commerce Line Item: Line item is of a product line item type (true).
If I go forward to the review and then back to the cart, the shipping cost appears, but I only have one flat rate service, so it make sense to show it all the time in the cart.

kind regards,
Crouchie

nicolas bouteille’s picture

Hi Crouchie,

A Commerce line item can concern products but also shipping services for example. The filter you are mentionning thus is exactly the one you need to disable. So that the line items displayed in the cart are not only products but also shipping informations...

Note that if you previously added a relation to the product like I did in order to display the product type, then you need NOT to Require this relationship. Indeed, this would act as well as a filter that would display only Commerce line items of type product...

Last, because you will probably need to have a different presentation for the products and the shipping line (no quantity field, no remove button, etc.) I would recommand to create a separate display and actually embed it just before the cart summary in the view footer. Haven't tested it yet however.

nicolas bouteille’s picture

Ok finally, I found that the best way to show the shipping price in the cart form is to remove the "Cart summary" widget from the view footer and to replace it with the "Order Total" widget that nicely displays:
sub-total
shipping price
total

blacklabel_tom’s picture

Issue summary: View changes
StatusFileSize
new1.19 KB

Hi All,

I've added an updated version of fargos rule that will always re-calculate the shipping on the checkout and review page.

In my use case the shipping changed based on the quantity and users were able to go back and add extra items without the shipping cost getting bumped up accordingly.

Cheers

Tom

rooby’s picture

With the rules here I am still unable to get shipping to appear on the cart contents table when I first go to the checkout page.

It seems that the calculations happen after that pane has been rendered so I don't get the value until the next page load.

Adding the cart order status to the conditions also didn't seem to help.

Does anyone have a solution that would work for my use case?

rooby’s picture

Ah the problem was the shipping address condition.
Since my shipping rates are not dependent on shipping I removed that and it works as I need it to.

maxplus’s picture

Hi,

I'm also using the solution of Nicolas Bouteille from #21 in my checkout progress.

I'm showing a custom cart block in a sidebar during the checkout progress.
It is not perfect because I don't have a live-update of my cart block, but is is OK for now

Thanks

Frederic wbase’s picture

StatusFileSize
new1.02 KB

I've changed the rules so that it applies the first availabe shipping method. Also now it works together with discounts and coupons. See attachment.

Also don't fotget to disable the rule "Delete shipping methods on cart update" as described in https://www.drupal.org/node/1270112

Maybe it's time to close this issue?

Further more i think it would be a good idea to open a new issue with a general status update of this module and a module version witch has some patches applied so that we create a new dev faster?

roball’s picture

Title: automatically apply a shipping rate via Rules » Rule to Auto-add shipping service
StatusFileSize
new1014 bytes

Attached is an updated rule code to be used for importing the rule "Auto-add shipping service".

It is based on #26, but has corrected the data selector of the actions to "commerce-order" (was "commerce_order"). It also removes "Shopping cart" from the commerce-order:status condition.

It should be noted that, unlike stated in #26, the rule "Delete shipping line items on shopping cart updates" (introduced by the "Commerce Shipping" module) must be kept active.

The rule "Collect rates: Flat rate" (introduced by the "Commerce Flat Rate" module) must be disabled. Instead, the imported rule "Auto-add shipping service" must be active.

Frederic wbase’s picture

@roball: i don't know what your shipping method is, but if use your rule and enable the rule to delete shipping line items + disable the rule collect rates then it doesn't work anymore in combination with coupon codes or discounts that add free shipping... Als when you have multiple flat shipping services for multiple countries and you change your country during checkout the shipping costs aren't updated...

What version do you use of commerce shipping & flat rate?

roball’s picture

@Frederic wbase, I am successfully using the latest releases of Shipping & Shipping UI (7.x-2.2) and Flat Rate (7.x-1.0-beta2), and thus are using only the Flat rate Shipping method. I have defined two Shipping services: Shipping of Book within EU and Shipping of Book outside EU. Only one of them can be applied, depending on the country the customer chooses from her Billing information customer profile. The correct shipping service is auto-added when the customer changes the country during checkout.

krembo’s picture

If you use some conditions, say order total is less than 200, it doesn't work.
I tried to add this condition to Auto-add shipping service rule, but still no luck.
If shipping was added once it stays there no matter what.

Frederic wbase’s picture

@robal thanks for the update. Do you also use coupons & discounts that apply free shipping?

sander wemagine’s picture

The rule above did work, but if customer navigated from 'checkout review' to 'checkout' the shipping costs would sometimes change if your shop has multiple shipping services based on the order total.

This rule below based on #27:

  • Auto selects the first available shipping service
  • Displays the shipping costs on the pages: cart, checkout & checkout review
  • Supports multiple shipping costs services.
  • In your shipping services you can create conditions like "Free shipping >€50" and "Shipping costs <€50"
{ "rules_auto_add_shipping_service" : {
    "LABEL" : "Auto-add shipping service",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_shipping", "entity" ],
    "ON" : { "commerce_order_insert" : [], "commerce_order_update" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "commerce-order:status" ],
          "op" : "IN",
          "value" : { "value" : {
              "cart" : "cart",
              "checkout_checkout" : "checkout_checkout",
              "checkout_review" : "checkout_review"
            }
          }
        }
      },
      { "data_is" : { "data" : [ "commerce-order:type" ], "value" : "commerce_order" } }
    ],
    "DO" : [
      { "commerce_shipping_delete_shipping_line_items" : { "commerce_order" : [ "commerce_order" ] } },
      { "commerce_shipping_method_collect_rates" : {
          "shipping_method_name" : "flat_rate",
          "commerce_order" : [ "commerce-order" ]
        }
      },
      { "commerce_shipping_rate_apply" : {
          "commerce_order" : [ "commerce-order" ],
          "shipping_service_name" : [ "" ]
        }
      }
    ]
  }
}
guypaddock’s picture

I tried the rules proposed in this thread, but all were very fragile if we needed to support multiple shipping options, or needed to change parts of the order that might change the shipping amount.

Instead, we wrote this module:
https://www.drupal.org/project/commerce_shipping_on_review