learn to setup magento 2 rules properly

Setting Magento Table Rates, Promotions and various restrictions is an important part of managing a fully-functional Magento/Magento 2 shop. That’s why our clients ask questions about Amasty module rules very often, and to help them use the rules logic properly, we collected several mistakes that are most often found in Amasty support queries.

Magento 2 Rules: Avoid Common ‘Rules Conflict’ Mistake

We call it ‘rules conflict’. Usually, it’s caused by incorrect condition filling and is one of the top-wanted cases in our support dept.

Problem

The problem emerges when you set up 2 non-overlapping restriction rules that are antagonists in nature. Below we address the situation in vivo example.

Say, you need to make Free Shipping available for all US states except Alaska, Hawaii and Puerto Rico. Alaska, Hawaii and Puerto Rico will have the flat rate method displayed instead of the free method. And only the flat rate method will be displayed for Alaska, Hawaii and Puerto Rico, not for other states.

You need to (a) enable Flat Rate and restrict Free Shipping for Alaska, Puerto Rico and Hawaii and (b) restrict Flat Rate and enable Free Shipping for all the states, except Alaska, Puerto Rico and Hawaii.

You go and set up Rule 1 to exclude Free Shipping to Hawaii, Alaska and Puerto Rico. And this works fine. Then you set up Rule 2 to hide Flat Rate shipping to any state except Alaska, Hawaii and Puerto Rico. This is not working at all. It is just hiding the flat rate method for all states.

The case seems logical, however, the conditions repeat the code branches and can return True if all the conditions or one of them is fulfilled. Thus, you messed up.

Analysis

There are no obstacles to restrict Free Shipping for Hawaii, Alaska and Puerto Rico.

And all the steps you made are right:

shipping rules setup to restrict free shipping to 3 states

Thus, you restrict any free shipping to any of the three states.

The problem emerges when you create Rule 2 or Flat Rate restriction for 48 states on the same basis:

problem-emerges-with-rule-setup-for-free-shipping

The difference between the previous and this screenshot is that you changed True to False. It seems logical, however, this is what you receive, as the result – Rule 2 worked correctly, restricted Flat Rate and returned Free Shipping:

setup correct rule work on magento 2

Rule 1 failed, restricted Free Shipping and didn’t activated Flat Rate:

rule in magento 2 failed to work on the frontend

Thus, you see that Rule 1 didn’t work and go to make edits to it. Having disabled Rule 2, you understand that Rule 1 works correctly. Then you proceed with Rule 2, you start sorting conditions or even change False to True and ‘is’ to ‘is not’. As a result, you change nothing in the work of the rules.

Solution

All you need is to go to Rule 2 and change Any to All:

changes any to all in rule in magento 2

Why is it so simple and so confusing?

When there’s Any the rule returned True for the first condition already and the code doesn’t execute further. Thus, the code partially worked for the first from the 3 states (e.g.: Hawaii) and didn’t at all for Alaska and Puerto Riko. This led to the case when Flat Rates was restricted as for Hawaii and New York that is for any other state.

Magento 1

1. ‘IS’ in condition with lists

IS in conditions - wrong variant
If you use conditions as in the picture above, Magento will search for ‘17,15,12,44’ category ID. Naturally, we don’t have such a category, and we actually need another result. Here’s the right variant:

IS in conditions - correct

Here Magento will be searching for these IDs: ’17’, ’15’, ’12’, ’44’ and the stated Amasty rule will work as desired.

2. Compare with zero

Compare with zero in Magento rules - wrong

At first sight this condition looks legit. But there’s a technical issue: basically, you should avoid comparing with zero everywhere in Magento, not only in sales or shipping rules.

The following condition is identical logically and, what is more important, it will work correctly.

Compare with zero in Magento rules - correct

 3. ‘greater than’ condition operator

Sometimes people use > or < symbols to describe amount comparison relations, but Magento won’t understand them properly. Use a special conditions operator instead:

Greater than conditions operator in MagentoGreater than conditions operator - correct

 

 

 

4. Range/list

Very often we notice that Magento shop owners use ‘range’ when they need Magento to apply something to a list of items. This is wrong, and Magento won’t understand this combination as a range. You need to list items (in our case – SKUs) one by one using a comma. And yes, ‘is/is one of’ case shown once again.
Range/list in Magento conditions
Range/list in Magento conditions - correct

However, there are a few clients who use manual listing of all the post-codes, therefore, for post-code ranges we normally use the operator ‘starts from’ and, if needed, ‘ends with’:

correct postcode range in magento 2

5. Default shipping address vs Shipping state/Province

Names can’t be used with default shipping address, you need to use the default shipping ID instead. Literal location names are used with Shipping state/Province entities.

Default shipping address condition in Magento - wrongShipping address condition in Magento - correct

 

 

 

 

Magento is a powerful rule engine but to use most of it you need to know how to configure it right.

We do hope this information will be helpful for you, and you’ll never make these mistakes again!

If you have any other questions on typical conditions logic mistakes, welcome to ask.