Videos

Removing Items From the Cart Using Rules

Date Published: 
01/26/2012
Duration: 
22:14

A member on DrupalCommerce.org was asking how to create a rule that would prevent users in New Mexico from purchasing slingshots. I thought about it for awhile and decided this technique would work.

1. Add a field to the product that marks it as prohibited.
2. Create a rule that loops through all the line items when the order is about to be saved. Have it call a component (rules subroutine) for each line item to see if that line item should be removed.
3. Create the component that does the work. It needs to take two arguments (line item and the order-about-to-be-saved)

This turns out to do the trick, but there's a lesson in rules debugging in there too :-)
The original question is at drupalcommerce.org/node/2491, and I'll post exports of the rules used there.