1

I use Drupal Commerce on Drupal 7 site, using product displays and products.

Each product has a shipping cost field.

At checkout, the shipping costs of all products in the order need to be collected and shown.

My approach is this: I use the Commerce Flat Rate module which creates the Flat Rate shipping method. I then create a flat rate shipping service ´Custom Shipping Cost´. Then I create a new shipping Rule ´Apply custom shipping costs´ which has a loop that needs to get the field value of the field ´custom shipping cost´ for each product in the order.

Question: how should a rule look like that gets the field values of a product connected to a line item and adds it to the shipping cost.

The following rules (which are nested) are not working. I figured out using Rules logs that in the third rule, Entity has field is not passed. I try to get the product field value which needs to be added to the total shipping rate.

0.528 ms Evaluating the action component_rules_get_shipping_price_field_value. [edit]
" Evaluating rule get local shipping price field value. [edit]
0 ms Evaluating rule get local shipping price field value.
0.049 ms Evaluating conditions of rule get local shipping price field value. [edit]
0.11 ms The condition entity_has_field evaluated to FALSE [edit]
0.113 ms AND evaluated to FALSE.
0.123 ms Finished evaluation of rule get local shipping price field value.

================================================

rule 1

rule2

rule 3

{ "rules_apply_custom_product_shipping_costs" : {
    "LABEL" : "Apply custom product shipping costs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_shipping" ],
    "ON" : { "commerce_shipping_calculate_rate" : [] },
    "DO" : [
      { "drupal_message" : { "message" : "Calculating shipping rates." } },
      { "variable_add" : {
          "USING" : {
            "type" : "commerce_price",
            "value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
          },
          "PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "Custom shipping cost" } }
        }
      },
      { "component_rules_get_local_shipping_rate" : {
          "price" : [ "custom-shipping-cost" ],
          "line_item" : [ "commerce-line-item" ]
        }
      },
      { "component_rules_get_international_shipping_rate" : {
          "price" : [ "custom-shipping-cost" ],
          "line_item" : [ "commerce-line-item" ]
        }
      }
    ]
  }
}

==================

{ "rules_get_local_shipping_rate" : {
    "LABEL" : "Set local shipping rate",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_line_item" ],
    "USES VARIABLES" : {
      "price" : { "label" : "price", "type" : "commerce_price" },
      "line_item" : { "label" : "line item", "type" : "commerce_line_item" }
    },
    "IF" : [
      { "data_is" : { "data" : [ "line-item:type" ], "value" : "shipping" } },
      { "data_is" : {
          "data" : [ "line-item:commerce-shipping-service" ],
          "value" : "local_shipping_rate"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Start loop: collect line item local shipping price field values" } },
      { "LOOP" : {
          "USING" : { "list" : [ "line-item:order:commerce-line-items" ] },
          "ITEM" : { "lineitem" : "lineitem" },
          "DO" : [
            { "variable_add" : {
                "USING" : {
                  "type" : "commerce_price",
                  "value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
                },
                "PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "custom shipping cost" } }
              }
            },
            { "component_rules_get_shipping_price_field_value" : {
                "custom_shipping_cost" : [ "custom_shipping_cost" ],
                "line_item" : [ "line_item" ]
              }
            },
            { "commerce_line_item_unit_price_add" : {
                "commerce_line_item" : [ "line-item" ],
                "amount" : [ "price:amount" ],
                "component_name" : "flat_rate_local_shipping_rate",
                "round_mode" : "1"
              }
            },
            { "drupal_message" : { "message" : "Single product shipping cost is" } },
            { "drupal_message" : { "message" : [ "custom-shipping-cost:amount" ] } }
          ]
        }
      }
    ]
  }
}

==============

{ "rules_get_shipping_price_field_value" : {
    "LABEL" : "get local shipping price field value",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "custom_shipping_cost" : { "label" : "custom shipping cost", "type" : "commerce_price" },
      "line_item" : { "label" : "line item", "type" : "commerce_line_item" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } },
      { "entity_has_field" : {
          "entity" : [ "line-item:commerce-product" ],
          "field" : "field_shipping_rate_local_countr"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "line item has field commerce_product" } },
      { "data_set" : {
          "data" : [ "custom-shipping-cost" ],
          "value" : [ "line-item:commerce-product:field-shipping-rate-local-countr" ]
        }
      }
    ]
  }
}

=========================== EDIT The following issue was causing my problem: In this whole rules setup i used multiple variables as line items. I mistakenly used a lineitem variable outside the loop, instead of the lineitem variable inside the loop. at a certain point i had lineitem and line-item and mixed them up which made it not work. Also, the product needed to be added as variable in the loop.

  • Bonjour! What have you tried, and presumably didn't work "yet"? Please include an export of the rule(s) you have so far. – Pierre.Vriens Feb 11 '18 at 9:15
  • @Pierre Vriens - it appears that in the third rule the condition ´entity has field´ (line item has field commerce_product) is FALSE. I wonder why since my line items of type ´product´ do have the commerce_product by default. – Yuri Feb 11 '18 at 12:27
  • Waw, your updated version of your question is a real question now, chapeau for this level of detail! Curious to hear to what extend my (possibly bizarre?) answer will get you going ... the bets are open ... – Pierre.Vriens Feb 11 '18 at 13:20
1

Below is an attempt to answer this (most important?) part of your question:

The following rules (which are nested) are not working. I figured out using Rules logs that in the third rule, Entity has field is not passed. I try to get the product field value which needs to be added to the total shipping rate.

  • In your 3rd rule, add another parameter, to also pass the "Commerce Product" that the line item is related to. Say you name that "product_related_to_item".
  • In your 2nd rule, pass the value of that extra parm (= product_related_to_item) to invoke the 3rd rule, using the line item token related to the product (for sure you'll have that in your 2nd rule).
  • In your 3rd rule, replace your 2nd "Entity has field" by a similar Rules Condition, but now referring to the (same) field for entity product_related_to_item (not sure if you still need your first Rules Condition then, homework for you ...).

By doing these corrections, the typical Rules magic (after you add such Rules Condition) applies again. E.g you can use their values in subsequent Rules Actions. And yes I know/realize, this may look like "this makes no sense", or "you're basically doiing the same thing". At least that's what I first thought myself just a few days ago when I ran into a similar issue. But sure enough, this was the solution (and it turned out this has to do with some limitation when processing "referenced entities (i.e. product referenced in a line item) in Rules.

  • I have tried but can´t get this to work. Can you give a very simple example rule that does your method? – Yuri Feb 11 '18 at 16:22
  • I would be very happy to give you my login details of the website so you can have a try. I just don´t know how to send you a personal message..how does that work here.. – Yuri Feb 11 '18 at 17:16
  • tried this in the third rule but also keep on hitting the False: i.imgur.com/rlt4861.png – Yuri Feb 11 '18 at 18:28
  • I see that there is another similar unanswered question..with alternative solutions but i dont think using terms will be a solution in this case..the shipping cost must be unique for each product drupal.stackexchange.com/questions/80362/… – Yuri Feb 11 '18 at 18:40
  • hey @Yuri, sorry to hear "cannot get this to work". Probably tomorrow (only) I can provide such sample. Thank you for the login details offer, but let us keep that until "if anything else fails" (if needed: check my profile to contact me). About the rule in the .png: it seems like you're still using 3 Rules conditions that all refer to the line item. My "main point" is that you should replace them with replacement tokens related to that "product_related_to_item" (as in my answer). If you have not tried that, could you let me know the results you get by using those tokens in your conditions? – Pierre.Vriens Feb 11 '18 at 18:42
1

The following approach worked, thanks to hints of Pierre Vriens, although my specific use case is a bit complex because it uses two product fields for local and international shipping. I also used extra variables here and there, to make tokens available in system messages..so not all variables are actually required to make this work.

enter image description here enter image description here enter image description here enter image description here enter image description here

  • 1
    Nice! Merci for sharing the final result, looks like you even used my suggested field name for "product_related_to_item". Just in case you want to slightly further improve your answer: if you could ALSO include an export of those rules, future readers can simply copy/paste such export from your answer (instead of eyeballing the various screenprints and retyping them all manually). – Pierre.Vriens Feb 14 '18 at 9:02
  • Thank you, yes I will do that too. By the way can I ask you yo have a look at this question I have no idea where to start debugging with that drupal.stackexchange.com/questions/253240/… – Yuri Feb 14 '18 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.