Pricing Rules Based on Other Products Already Ordered

I'm not sure what the best way to proceed is. What I want to do is set the price of a product to zero if another product has already been purchased. It's a one-to-one relationship.

For example, one free scarf for every hat purchased.

What I need to be able to do is say "if the number of hats in the cart is higher than the number of scarfs, then set the price for the scarf to zero."

I'm pretty clear on how to set the price to zero, but not sure how to approach the condition.

Any thoughts would be appreciated.


Posted: Dec 18, 2011


rfay Randy Fay on December 19, 2011

I can think of a couple of ways to do this, but haven't tried any of them.

First, you'll have to make sure that you have a way to know everything they've ordered. To do that, of course, you're going to have to require them to be logged in, I would think.

Then you may be able to follow the current user to the orders owned by the current user that are completed, and from there to the line items. I'm not sure you can do that, but if you can't I think it will be important.

A second way is to create a logging rule that logs products purchased by user into some entity or node.

A third way is to add a userreference field to products and add users to the product when they purchase. That would be pretty easy and might work just fine.

gauntlet on December 19, 2011

I think you're solving a more difficult problem than I have. I want the prices to change based on what you have in the current cart. It's per-order, not per-person.

All I need is a rule that says, if the number of X in the current user's cart is greater than the number of Y in the user's cart, the price of Y is multiplied by .5, or whatever the discount is.

Thanks again.

gauntlet on December 20, 2011

Thanks for your help.

The problem is that the number I want to compare to is variable. I want the rule to apply only if the number of X is greater than the number of Y in the cart. You only get one half-price scarf for every hat you've already ordered.

rfay Randy Fay on December 20, 2011

You can reach out into the product or elsewhere for actual configuration for your rule. You don't have to wire numbers into it. You can put the logic into the rule, but the data can come from elsewhere. I demonstrated this technique in [Commerce Discount Campaign Feature](http://www.commerceguys.com/resources/articles/252) screencast. See if that gives you some ideas. But go ahead and experiment.