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

Magento 2.1x – Add options in custom product attribute using backend

Adding values to a dropdown attribute in the Magento 2 admin means clicking Add Option and typing a value, once for every value. That’s fine for five colours, but not for fifty. Instead, I let the browser console do the clicking and typing. I wrote this for Magento 2.1 in 2017. Go to the attribute edit page using the Magento 2 admin panel (Stores > Attributes > Product > Edit attribute) Use the console – ctrl + shift + J (Cmd + Option + J on a Mac) Edit the JS array mimim and then paste the following code in the console. ...

October 14, 2017 · 2 min · Harshvardhan Malpani

How to Update Magento 2.1x to latest version via SSH

If your Magento 2.1.x store was installed with Composer, you don’t need to download a fresh copy of Magento or click through the Web Setup Wizard to move to a newer 2.1 release. The whole upgrade can be done from an SSH session with the handful of commands below. These steps were written for Magento 2.1 and the example upgrades to 2.1.9. Replace 2.1.9 with the version you are moving to. ...

September 26, 2017 · 3 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