Many times while selling products online you have to enable your shipping service for limited areas only. Due to Higher shipping/export rates, banned products, strict customs laws or tough cash handling this all results in admin to restrict order shipping to limited areas. Luckily, using Magento 2 you can create shopping cart rules that allow you to check for dynamic conditions

However, if you want to create your custom shopping cart rules that trigger specific code in reaction to that event. To do the same you have to create your custom extension to fulfil your business needs as we never recommended to modify core files. Let’s take an example of settings up custom sales rules for the city field of the shopping cart page. You can also create custom shopping cart rules for different events.

To do the same first, we need to create “events.xml” file inside etc folder and add paste below code in that file.

app\code\Vendor\Extension\etc\events.xml

Now, we create “Shippingcityconditionobserver.php” inside observer folder.

app\code\Vendor\Extension\Observer\Shippingcityconditionobserver.php

Create one more file “di.xml” file at the following path.

app\code\Vendor\Extension\etc\di.xml

Lastly, create one more file named “Shippingcity.php” at this location.

app\code\Vendor\Extension\Model\Rule\Condition\Shippingcity.php

Simply clear the cache and you will find an option in admin backend. You can also play with the above code to create different kinds of custom shipping rules.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

Happy Coding! ?

 

Click to rate this post!
[Total: 18 Average: 3.9]