I would like to offer a shipping service based upon there being fewer than a certain number of product line items per order (necessary because of a limited number of lines on a customs form), but haven't found a simple way to do this using Rules. I can get a count of all commerce line items, but that includes shipping, tax, etc. Similar is mentioned here. Would the ability to count the number of a certain type of line item be an appropriate addition to this module? Thanks for all the work you've already done.
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | commerce_rules_extra-total_line_count-2370715-37.patch | 2.84 KB | perignon |
Comments
Comment #1
svouthi commentedComment #2
perignon commentedAny improvements is always welcomed!
Are you trying to find the total number of items in a cart? Made of multiple SKUs?
Comment #3
svouthi commentedThank you for your prompt reply. Yes, I am. I thought a list count comparison would provide the desired total, but I don't see a token like site:current-cart-order:product-line-items - just site:current-cart-order:commerce-line-items, which includes the shipping, tax, etc. I could use the latter token except that the line items provided by Commerce Fees and Commerce Shipping on my Review Order page remain when a user returns to a previous checkout page and so are counted subsequently when the Shipping page is reached. I've posted in existing Commerce Fees issue queues here and here in hopes of removing the shipping line item when a user backs out of checkout. The ability to obtain a list count of only product line items would appear much simpler.
Comment #4
perignon commentedYou point out the problem with attempting to count line items because of how Commerce works, everything is a line item to include more than just shipping. Taxes and discounts are also line items.
I have built a rule to count the number of products in an order to provide a discount. I wrote a rule that does partial SKU matching (how I got involved and took ownership of CRE). I had the problem where I wanted to discount clothing based on the number of shirts that were bought, well the shirts in our shopping cart totaled around 300 SKUs since every variant of a shirt has a unique SKU. So I wrote the rule to match partial SKUs. Since we standardize our SKUs based on product type I was able to match all clothing in our shopping cart by matching the first 3 characters of the SKU.
I think using SKU matching is the most reliable method to count the amount of products in a shopping cart because various contributed modules to Commerce add new line items to a cart. I run commerce discount and commerce coupon, both of those also add line items to a cart. So it's impossible to account for all the various line items if you were to attempt to write a rule to disregard line items. I think the best course of action is to target the line items you want to count via SKU matching.
One thing I haven't done with the partial SKU matching that I wanted to do, was offer REGEX type comparison against the SKU.
Comment #5
svouthi commentedYes, however there are types of line items, and since Product exists as a line item type out of the box (with Commerce Kickstart at least), I thought it would be logical to be able to get a count of that particular line item type (versus custom line items) simply. I don' t know that matching SKUs is viable for me since I have such a variety of them and they aren't determined in house.
Comment #6
perignon commentedMaybe there is a possibility. I would need to revisit the Product entity as it comes through the cart and exposed to rules.
But the SKU matching is what I used to do this. I have control over my SKU's and hence have them controlled to be very logical. Even with third party sales I dictate the SKU to them, they don't get to determine their SKU. In the end it allows me to keep products organized via SKU.
If you are not determining SKUs in house, I would suggest a change to your business logic. Allowing a third party to dictate SKU numbers does nothing but cause you a world pain and anguish.
Comment #7
svouthi commentedI understand that using all our own SKUs gives maximum comfort and control and the capability you describe, however our industry is such that customers search for our products by their standard SKU and expect to see it on their invoices. Rather than coming up with an entire alternate SKU system of our own and effecting the display/printing of the industry standards SKUs, I'd rather just count the product line items if possible. I appreciate you taking a look at Commerce's workings to see what's possible.
Comment #8
perignon commentedYeah in that case you would need two SKU fields.
I got a backlog of stuff and just took on a new project with 1 week sprints to a prototype in December so I am stretched thin at the moment. How important is this for you?
Comment #9
svouthi commentedThis is the last rule I need to figure out how to compose before my site is ready to launch. I'd be happy to give you a little compensation if you could find time to provide a site:current-cart-order:product-line-items token or similar before the end of the month. Thanks for asking.
Comment #10
perignon commentedOh crap. I'll see what I can get cooking then. I know how deadlines are!
Comment #11
svouthi commentedI appreciate that, Perignon. All the best with yours too!
Comment #12
perignon commentedI started working on a new condition to do this. Still got to look into the order object and line items. It may require input of product type names.
Comment #13
svouthi commentedThanks for the report. I'm sorry if it isn't as simple an undertaking as I'd hoped.
Comment #14
perignon commentedStill got this on my scope, just haven't done anything in the last 10 days.
Comment #15
svouthi commentedI know you're busy. I appreciate you picking it up at all.
Comment #16
perignon commentedI actually think I found a way to do this. Took digging around in the Order object, no idea what I would do without drush php-eval!
It seems all products in a shopping cart is labelled as a line item type "product". I tested this against a completed cart that had a shipping value on it. I am about to test the sales tax modules to see if I get the same result. If so I think I found an easy way to do this and it's basically a version of the rule I wrote to find partial SKU matches.
I been doing a lot of work on various modules on D.O and it took me a while to get this. I was trying to get the Freshdesk module released and I finished that on Friday. And I was playing wack-a-mole in the Storage API queue that I recently took over as co-maintainer.
Comment #18
perignon commentedI just commited a new condition to the dev branch.
This condition will allow you to compare the total number of products in the shopping cart. See if this fits the bill. It was relatively easy after digging around in the API...
Comment #19
svouthi commentedThanks, I just updated to the latest dev (7.x-2.1-beta4+1-dev) and saw two new conditions:
Order has one or more products where the sku contains
Total quantity of products in the cart
I assumed "Total quantity of products in the cart" was the one to target for my use case, so I tried that, but it behaves like "Total product quantity comparison" in that when I increase the number of one line item in the cart, the rules event isn't triggered. I'm looking for a count of the total number of product line items rather than the total number of products. Let me know if there's something I missed.
Comment #20
perignon commentedCan you give me an example to work with so I completely understand the use case. I may can that rule because it's honestly a dupe.
Kind of example I am looking for is something like this. Describe the cart contents and tell me what the value would be for that cart
Would the value you would want to extract be 5 or 3?
5 would be what the current rules produce. I think you are wanting the 3 right? If so I know how to do that and can get it done in a few minutes when I get home tonight from my day job.
Comment #21
svouthi commentedYes, you're right, I am looking for the value three in my use case. Glad that's fairly simple to get to. I appreciate you spending time on it.
Comment #22
perignon commentedAre you comfortable with patches?
Comment #23
svouthi commentedYes, I am.
Comment #24
perignon commentedThrew this together fast before work. Give it a shot. It just counts the line items that are of the type "product" but only counts one per line item regardless of the quantity.
Comment #25
svouthi commentedThanks so much - I will. Sorry to take a moment, my life just got busier again - will report tomorrow.
Comment #26
perignon commentedNo worries. I am heading out on a weekend vacation out of town for 5 days so I may not be quick to respond here on D.O.
Comment #27
svouthi commentedGood for you. I apply patches manually and so created the new .inc file. I'm seeing an error:
Fatal error: Call to undefined function commerce_rules_extra_compare_line_item_count_condition_info() in sites/all/modules/commerce_rules_extra/commerce_rules_extra.rules.inc on line 30
Comment #28
perignon commentedWoopps Typo.. Try this one.
Comment #29
svouthi commentedHmmm - I'm still getting the same error, but now only once I get to Rules and try to add a condition.
Comment #30
perignon commentedMaybe third time is the charm... Maybe not..
Comment #31
svouthi commentedHmmm, unfortunately still seeing the same error. Cleared caches just in case, but no luck. Thanks for dealing with this just now.
Comment #32
perignon commentedYeah huge goof. I named the internal function wrong. It has to match the file name. I wrote the rule without even attempting to load it into Drupal at all. I at least got Drupal to load with this patch. I did not build a rule yet to test it that thoroughly - I'm still on vacation in London.
Comment #33
svouthi commentedCiao,
Yes, this file succeeded in loading the condition. I changed the label to "Total quantity of product line items in the cart" in order to differentiate it from the other "Total quantity of products in the cart" condition. However, the page after selecting the new condition just displays the checkbox for "Negate - If checked, the condition result is negated such that it returns TRUE if it evaluates to FALSE." and no settings for making a quantity comparison. If I just click save (without checking the box), I get "Unknown condition commerce_rules_extra_compare_line_item_count." So, much improved, but still no dice. I hope you are enjoying your vacation and only dealing with this when you find yourself not into doing anything else. Thanks again.
Comment #34
perignon commentedHrm..... that function does exist. With Rules you often have to delete your work and start over when you introduce new code. When I return home I will check the rule from a functionality perspective. I'm on the train to the airport to return home tomorrow.
Comment #35
svouthi commentedHmm, indeed. Thanks, Perignon, and good travels.
Comment #36
perignon commentedI just ran through a test on my local machine here, got an hour on a train ride in the dark (nothing to see), and I got the right behavior. I tested a non-true and a true case and got the correct results. I tested by using the action under "System" to display a message on the site.
Comment #37
perignon commentedJust made some of the description more clear. No functional changes in this patch.
Comment #38
svouthi commentedHooray! Looks like it works. I deleted the old .inc file, cleared my caches, loaded the new file and created a new rule using the condition but still received the "Unknown condition..." message. It was only after running update.php (with no pending updates) that I finally was able to view the full "Editing condition..." page. Thank you so much.
Comment #39
perignon commentedAwesome. I will role this into the Dev branch
Comment #40
perignon commentedPushed into dev and deleted the other rule created here in this issue as it was a duplication of an existing rule.
Comment #42
wdseelig commentedIs there an easy way to count the number of instances of a SKU in a shopping cart? In my use case, I have a SKU for a custom line item, and just want to be able to use the number of instances of that SKU in a rule.
Comment #43
perignon commentedHrm. Possibly. There are a lot of rules associated with SKUs.
Comment #44
wdseelig commentedThanks Perignon for commenting
I've been breaking my head on this for a while. If you even have a hint, I would appreciate it.
Comment #45
mywebmasteruk commentedI am also looking for a way to count the number of line items (one item quantity per line item) so I can record on the order the total number of products bought