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.

Comments

svouthi’s picture

Issue summary: View changes
perignon’s picture

Any improvements is always welcomed!

Are you trying to find the total number of items in a cart? Made of multiple SKUs?

svouthi’s picture

Thank 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.

perignon’s picture

You 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.

svouthi’s picture

Yes, 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.

perignon’s picture

Maybe 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.

svouthi’s picture

I 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.

perignon’s picture

Yeah 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?

svouthi’s picture

This 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.

perignon’s picture

Oh crap. I'll see what I can get cooking then. I know how deadlines are!

svouthi’s picture

I appreciate that, Perignon. All the best with yours too!

perignon’s picture

I 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.

svouthi’s picture

Thanks for the report. I'm sorry if it isn't as simple an undertaking as I'd hoped.

perignon’s picture

Still got this on my scope, just haven't done anything in the last 10 days.

svouthi’s picture

I know you're busy. I appreciate you picking it up at all.

perignon’s picture

I 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.

  • Perignon committed 899231d on 7.x-2.x
    Issue #2370715: Provide for the ability to count the number of a certain...
perignon’s picture

Assigned: Unassigned » perignon
Status: Active » Needs review

I 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...

svouthi’s picture

Thanks, 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.

perignon’s picture

Can 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

Cart A
Red Shirt  Large   Quantity 1
Blue Shirt Small    Quantity 2
Orange Shirt Medium Quantity 2
Shipping
Tax

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.

svouthi’s picture

Yes, 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.

perignon’s picture

Are you comfortable with patches?

svouthi’s picture

Yes, I am.

perignon’s picture

Threw 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.

svouthi’s picture

Thanks so much - I will. Sorry to take a moment, my life just got busier again - will report tomorrow.

perignon’s picture

No 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.

svouthi’s picture

Good 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

perignon’s picture

StatusFileSize
new2.92 KB

Woopps Typo.. Try this one.

svouthi’s picture

Hmmm - I'm still getting the same error, but now only once I get to Rules and try to add a condition.

perignon’s picture

StatusFileSize
new2.93 KB

Maybe third time is the charm... Maybe not..

svouthi’s picture

Hmmm, unfortunately still seeing the same error. Cleared caches just in case, but no luck. Thanks for dealing with this just now.

perignon’s picture

StatusFileSize
new2.92 KB

Yeah 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.

svouthi’s picture

Ciao,

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.

perignon’s picture

Hrm..... 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.

svouthi’s picture

Hmm, indeed. Thanks, Perignon, and good travels.

perignon’s picture

I 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.

perignon’s picture

StatusFileSize
new2.84 KB

Just made some of the description more clear. No functional changes in this patch.

svouthi’s picture

Hooray! 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.

perignon’s picture

Awesome. I will role this into the Dev branch

perignon’s picture

Status: Needs review » Fixed

Pushed into dev and deleted the other rule created here in this issue as it was a duplication of an existing rule.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

wdseelig’s picture

Is 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.

perignon’s picture

Hrm. Possibly. There are a lot of rules associated with SKUs.

wdseelig’s picture

Thanks Perignon for commenting

I've been breaking my head on this for a while. If you even have a hint, I would appreciate it.

mywebmasteruk’s picture

I 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