Tags/topics: 
1
Answers
Vote up!
0
Vote down!

How to deactivate a specific shipping option if at least one of the products in the cart is assigned a taxonomy term?

I've created a taxonomy vocabulary with "term1" and "term2" in it. Every product in my commerce shop is marked with either one or the other term.

What I want is to hide a specific shipping service if at least one of the products in the customer's cart is marked with "term1", no matter if or how many products are assigned the other taxonomy term. Is that possible?

Asked by: sagan_gr
on November 10, 2012

1 Answer

Vote up!
0
Vote down!

Hi,

This should be fairly simple. It may be easier if we turn the problem on it's head - think of it as activating the said shipping service if and only if there are NO products tagged with "term1":

- Lets say you have a given custom shipping service "A" defined.
- "A" is applied conditionally by means of rules. So,
- Select "Configure component" from your list of services.
- Now you can add conditions and actions:
- Add a condition "Execute custom PHP code" and within this write a simple bit of code to checks that there are no products tagged with "term1", and if so return TRUE.
- Add an action "Add a rate for a shipping service to an order" and configure it to add "A".
- Save, and test...

Now, all being well with your logic (see below), you will find that "A" is only added in the context where there are no products tagged with "term1" in the cart.

(As for how to write the logic required to check the taxonomies on products, my first attempt would be:
- load current user or anon. order (this will probably be given in 'execute code' context anyway),
- examine line items of type product or product display and check their taxonomies.
- You could probably do this quite quickly with EntityMetadataWrapper and taxonomy API.

I'd also recommend moving that bit of logic into a method that sits within your code base, or before you know it half your business logic will be squirreled away in execute PHP code fields - I hate that. Yes you could make a feature of the rule anyway, but by just calling a method external to the rule, you keep your business code together and tidier.)

Let me know how you get on :)

Answer by: jphil
Posted: Nov 25, 2012