Insert Indian States in Magento Database

Magento ships with lists of states or regions for some countries, but when I wrote this India wasn’t one of them. For a country without a region list, customers get a free-text “State/Province” box instead of a dropdown, so the same state ends up spelled several different ways across orders, and anything that depends on the state, such as tax rates per state, is harder to set up. The query below adds the Indian states and union territories so they show up as a dropdown at checkout, in customer addresses and in the admin. ...

November 1, 2017 · 3 min · Harshvardhan Malpani

How to create Magento 2 category programmatically?

Creating a handful of categories in the admin is fine, but when you’re setting up a new store or migrating a category tree from another system, clicking through the form for each one gets old quickly. This standalone PHP script boots Magento 2 and creates categories with a single function call each. Before you start Save the script as a PHP file (for example create-category.php) in the Magento root folder, the one that contains app/ and bin/. It includes ./app/bootstrap.php, and that path is resolved from the folder you run it in. Run it from the command line with php create-category.php, as the user that owns the Magento files. Find the ID of the parent category you want to use. Go to Catalog > Categories in the admin and select the category; its ID is shown next to its name at the top of the form. Use this code: ...

October 27, 2017 · 3 min · Harshvardhan Malpani

How to secure a folder by whitelisting one IP using HTACCESS and denying all others

If a folder such as /admin should only ever be used from one place, say your office, you can lock it to that IP address in .htaccess, so everyone else gets 403 Forbidden before the admin login page even loads. This post does it with mod_rewrite, and then adds a small PHP script that updates the allowed IP for you, which helps when your connection’s IP changes from time to time. ...

October 26, 2017 · 4 min · Harshvardhan Malpani

Start a magento instance programmatically

When I need to run a one-off job on a Magento 1 store, such as fixing data in bulk or trying out a model, I don’t want to build a whole module for it. A standalone PHP file that loads Magento and then runs my code is enough. This is the bootstrap I put at the top of such a script. It’s for Magento 1, which has app/Mage.php; Magento 2 starts up differently, through app/bootstrap.php. ...

March 17, 2017 · 2 min · Harshvardhan Malpani

Flushing Magento cache – The Programmer way

This is for Magento 1. Usually you clear the cache from System > Cache Management in the admin, but sometimes you need to do it from code: at the end of a deployment or import script, in a cron job, or after a script changes configuration directly in the database. This one line does it: Mage::app()->getCacheInstance()->flush(); What it does Mage::app() returns the running application, getCacheInstance() returns its cache object, and flush() empties the whole cache storage. It’s the same thing the Flush Cache Storage button in Cache Management does. ...

March 13, 2017 · 1 min · Harshvardhan Malpani

Commonly used regular expressions (regex) for developers in PHP

Commonly used regular expressions (regex) for developers in PHP Regex are used or can be used almost everywhere strings are involved. If you are dealing with text strings, you would often find patterns in them and you might be required to filter few of them based on some similarities and properties. For example: a currency or money is often written in this form – [Currency symbol] [Digits] ([.] [Digits])maybe Now, in order to filter any such things programmatically using a script, few of really helpful regular expressions are mentioned below: ...

May 15, 2016 · 3 min · Harshvardhan Malpani