0

I'm attempting to calculate an estimated shipping date on products with a turn-around time. Using a variety of fields on separate entities I've been able to get about 75% if the way. I run into a problem when attempting to perform the final calculation on my rule component. For some reason my calculated variable is not available on Input value 2 of the calculation when enabling data selection.

This is what my component looks like:

+ Set a data value:  
  Parameter: Data: [order:field-due-date], Value: [site:current-date]  
+ Add a variable: 
  Parameter: Type: Date, Value: now
  Provides variables: Shipping Date (shipping_date)  
+ Loop:  
  Parameter: List: [order:commerce-line-items]  
  List item: Current Line Items (line_item_list)  
    + Apply an interval to a date:  
      Parameter: Interval number: [product-display:field..., Interval period: Days, Date: [site:current-date]  
      Provides variables: Line Item Date (line_item_date)

At this point I try to add an action "Calculate a value" to the loop; like this:

+ Input Value 1
  Data selector (Select data of the types Decimal number, Date.)
  -- line-item-date --
+ Operator
  Value
  -- max --
+ Input Value 2
  Data selector (Select data of the type Duration.)
  -- shipping-date --

I calculate this in order to overwrite the shipping date with the highest "line-item-date" in order to display an estimated shipping date based on the product with the longest turn-around time.

Here's my problem: the "shipping-date" variable is not available in "Input Value 2". I don't understand why the "select data of type" is "Duration" and not "Decimal number, Date"; although I assume that's where the problem is.

0

I ended up using a slightly different approach. Just in case somebody else is interested in this I'm posting my final solution here.

PROBLEM

"Estimate" a ship date based on the turnaround times of all the products in cart whenever a product is added or removed from the cart and at checkout (just in case the customer decides to check out days after adding the products).

Additional modules and fields:

  • Using the interval field module I added a "Turnaround Time" field to the product variation type.
  • I also added a date field to the order entity - "field-due-date".

RULES

I created 3 items under rules.

Reaction Rule: Set the estimated shipping date

+ Events
    + After adding a product to the cart
    + After removing a product from the cart
    + Completing the checkout process
+ Actions
    + rule set: Set the estimated shipping date
        // these settings ensure that you run through each and every line item, instead of just the current item; spend a long time doing that - over and over. :(
        + Line Item
          Data Selector: commerce-order:commerce-line-items:0
        + Order
          Data Selector: commerce-order
        + Commerce Product
          Data Selector: commerce-product

Ruleset: Get Line Item Turnaround Value
rules_get_line_item_turnaround_value
This rule gets the integer value of each line-item's turnaround time (in days in my case) and writes it to "line-item-turnaround-time" variable which is passed along to the next ruleset. I could probably have used a regular Rule instead of a Rule Set here.

Date Type
    Commerce Line Item, Line Item, line_item, Parameter
    Commerce Order, Order, order, Parameter
    Commerce Product, Product, product, Parameter
    Integer, Line Item Turnaround Time, line_item_turnaround_time, provided

+ Rule: Get the Line Item Turnaround Time as a Number
  Provides variables: Line Item Turnaround (line_item_turnaround)
    + Conditions
        + Entity has field
          Parameter: Entity: [line-item], Field: commerce_product
          //exposes the line item to the rule
        + Entity has field
          Parameter: Entity: [line-item:commerce-product], Field: field_turnaround_time
          //exposes the line item's product variation to the rule
    + Elements
        + Add a variable
          Parameter: Type: Integer, Value: [line-item:commerce-product:field-turnaround-time:interval]
          Provides variables: Line Item Turnaround (line_item_turnaround)
          //pulls the turnaround time as an integer
        + Set a data value
          Parameter: Data: [line_item_turnaround_time], Value: [line-item-turnaround]
          //write's the turnaround time to the "line_item_turnaround_time" variable

Ruleset: Set the estimated shipping date
rules_set_the_estimated_shipping_date
This rule pulls the turnaround time from a list of the line items in the current order, compares them and uses the largest value to adjust the estimated delivery date. Again, I could probably have used a regular Rule here instead of a Rule Set.

Date Type
    Commerce Line Item, Line Item, line_item, Parameter
    Commerce Order, Order, order, Parameter
    Commerce Product, Product, product, Parameter

+ Rule: Calculate the estimated shipping date
  Provides variables: Order Turnaround Time (order_turnaround_time), Conversion result (conversion_result), New Estimated Shipping Date (new_estimated_shipping_date)
    + Conditions
        + Entity has field
          Parameter: Entity: [line-item], Field: commerce_product
          //exposes the line item to the rule
        + Entity has field
          Parameter: Entity: [line-item:commerce-product], Field: field_turnaround_time
          //exposes the line item's product variation to the rule
        + Entity has field
          Parameter: Entity: [order], Field: field_due_date
          //exposes the current order to the rule
    + Elements
        + Set a data value
          Parameter: Data: [order:field-due-date], Value: [site:current-date]
          //set the order's due date to the site's current date. This does two things: 1. ensures that the shipping date is based on the exact time the order is completed, as opposed to a previously calculated value, or the order creation date. 2. rules fail if no value is set
        + Add a variable
          Parameter: Type: Decimal number, Value: 0
          Provides variables: Order Turnaround Time (order_turnaround_time)
          //set the base turnaround time to 0 so we have something to compare our line item turnaround times to
        + Loop
          Parameter: List: [order:commerce-line-items]
          List item: List of Line Items (list_of_line_items)
          //loop through a list of the current order's line items
            + rule set: Get Line Item Turnaround Value
              Parameter: Line Item: [list-of-line-items], Order: [list-of-line-items:order], Product Display: [line-item:commerce-product]
              Provides variables: Line Item Turnaround Time (line_item_turnaround_time)
              //runs the "Get Line Item Turnaround Value" and pulls the "line_item_turnaround_time" for the current loop line item
            + Calculate a value
              Parameter: Input value 1: [line-item-turnaround-time], Operator: max, Input value 2: [order-turnaround-time]
              Provides variables: Calculation result (result)
              //check to see if the current loop line item's turnaround time is larger than the value of the order-turnaround-time
            + Set a data value
              Parameter: Data: [order-turnaround-time], Value: [result]
              //if the current loop line item's turnaround time is larger than the value of the order-turnaround-time write it as the new "order-turnaround-time" otherwise keep the old value
        + Convert data type
          Parameter: Target type: Integer, Value to convert: [order-turnaround-time], Rounding behavior: Always up (9.5 -> 10)
          Provides variables: Conversion result (conversion_result)
          //converts the order-turnaround-time to an integer, just in case it's not, so we can apply it as an interval
        + Apply an interval to a date
          Parameter: Interval number: [conversion-result], Interval period: Days, Date: now
          Provides variables: New Estimated Shipping Date (new_estimated_shipping_date)
          //add the new "order-turnaround-time" to the "site:current-date[field-due-date]" to calculate the estimated shipping date
        + Set a data value
          Parameter: Data: [order:field-due-date], Value: [new-estimated-shipping-date]
          //write the new estimated shipping date to the field-due-date field on the order

Hope that saves someone a couple of hours.

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.