Auto add shipping line item Drupal Commerce

by Jonathan Hindi   Last Updated November 30, 2017 17:07 PM

I want to auto add flat rate shipping cost to any order and display it in the shopping cart summary.

I am using Commerce Shipping and Commerce Flat Rate modules. I can't know how to implement it using rules.

I found this post but it didn't help me.

what I need is to automatically add $10 on every order for shipping, I will disable the Shipping Service Panel.



Answers 2


You can do this with Rules. Here is an example:

{
    "rules_auto_add_shipping_service" : {
      "LABEL" : "Auto-add shipping service",
      "PLUGIN" : "reaction rule",
      "OWNER" : "rules",
      "REQUIRES" : [ "rules", "commerce_order", "commerce_shipping", "entity" ],
      "ON" : { "commerce_order_update" : [] },
      "IF" : [
        { "data_is" : { "data" : [ "commerce-order:type" ], "value" : "commerce_order" } },
        { "commerce_order_contains_product" : {
            "commerce_order" : [ "commerce_order" ],
            "product_id" : "YOUR-SKU-HERE",
            "operator" : "\u003E=",
            "value" : "1"
          }
        }
      ],
      "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" : [ "" ]
          }
        }
      ]
    }
}

Just change the SKU or the other conditions to suit your needs.

nvahalik
nvahalik
October 27, 2015 18:43 PM

I have been using a rule similar to this to auto calculate and add shipping to the shopping cart. I have one problem which I can't seem to find a solution. My rule correctly calculates shipping for any products added to cart but it does not calculate correctly when there are multiple quantities of same SKU or if you change quantity in the cart. I does re-calculate correctly when you go to checkout review, though. I just can't get it to update with the correct shipping in the cart view. Is this a problem with my view, or the calculation? Here is my rule:

{ "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" : [ "" ] } } ] } }

Thanks for any help with this, and let me know if you need any other info.

Bill Hanson
Bill Hanson
November 30, 2017 16:33 PM

Related Questions


Add multiple cart in drupal 7

Updated August 15, 2015 17:03 PM




How to create custom commerce order states?

Updated May 17, 2015 08:03 AM