1
Answers
Vote up!
0
Vote down!

Separate Line Items based on Quantity

I am working with connecting my Commerce and Commerce Registration website with Salesforce and I need to get some information from the registrations into the line item in order to map properly (there is no many-to-many or one-to-many mapping options in the current salesforce drupal suite..)

In order for this to work initially, I need every registration product that get's added to the cart to be separated. This is naturally done for separate SKUs, but I am focusing more on the same SKU added to the cart with a QTY larger than 1, get separated out into different line items with their own ID.

I have looked around and mainly found the same answers which non have worked (Turn off the "combine liked products.." in all the product/line item displays). Other Rule based options I have tried work for the initial add to cart, but if someone then updates the qty on the cart page it breaks or if they then go back to the product and re-add it, it messes up the amount of line items in the cart.

My goal to to be able to separate them, add an Attendee field to the Line Item, then after saving the registration entity it should populate that Attendee field on the Line Item with the email address or Full Name of the registrant. This way I can push that info to Salesforce in a single Line Item -> Order Product mapping.

Any help would be greatly appreciated.

Travis
Asked by: Travis
on August 11, 2015

1 Answer

Vote up!
0
Vote down!

For anyone looking to do this (which there are a bunch of ppl..) I found a way to finally get this to work.

For starters, turn off the Combine options in your Line Item displays and Product displays.

Then install Rules Array Create module, this is key, since we need to work with a loop in Rules and the Quantity is a variable not an Array which is the only thing loops can work with.

Next, use import this Rule.

{ "rules_split_quantity" : {
    "LABEL" : "Split Quantity",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_array_create", "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "quantity" ], "op" : "\u003E", "value" : "1" } }
    ],
    "DO" : [
      { "array_create" : {
          "USING" : { "size" : [ "quantity" ] },
          "PROVIDE" : { "array_added" : { "amount_array" : "Amount Array" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "amount-array" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "commerce_cart_product_add_by_sku" : {
                "USING" : {
                  "user" : [ "site:current-user" ],
                  "sku" : [ "commerce-product:sku" ],
                  "quantity" : "1",
                  "combine" : "0"
                },
                "PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
              }
            }
          ]
        }
      },
      { "list_remove" : {
          "list" : [ "commerce-order:commerce-line-items" ],
          "item" : [ "commerce-line-item" ]
        }
      }
    ]
  }
}
Travis
Answer by: Travis
Posted: Aug 17, 2015

Comments

Just to add you probably noticed the "List Remove" action. This removes the original line item with it's original quantity. Without this, if you added a product with qty 5, you would end up with 6 line items. 1 with a qty of 5, and another 5 with the qty of 1.

I also changed the cart View display to only show the Quantity, and not the Quantity Text which allows people to further change their qty in the cart.

- Travis on August 17, 2015