Remove Compare feature from Magento 2

How to remove “Add to Compare” links in sidebar and product page in Magento 2 product pages Magento 2 Community has a default feature of comparison lists for multiple products. Learn how you can disable the “Add to Compare” feature in magento2 community edition. Many stores don’t need it, and the links just add clutter to product pages and listings. There’s no admin setting to switch it off, so the way to hide it is to remove its blocks in your theme’s layout. ...

December 20, 2017 · 2 min · Harshvardhan Malpani

CORS policy for subdomains – htaccess – apache server

Browsers won’t let a page on one origin read a response from another origin (load a web font, or read the result of an AJAX call) unless the other server says it’s allowed, with an Access-Control-Allow-Origin header. On a Magento store this shows up as CORS errors in the browser console when the shop, its static files and other parts of the site live on different subdomains. The catch is that Access-Control-Allow-Origin takes exactly one origin or *. You can’t list several subdomains in it. The rules below get around that: Apache checks the Origin header of each request against a pattern and, if it matches, sends that same origin back as the allowed one. ...

December 20, 2017 · 3 min · Harshvardhan Malpani

How to manage Magento 2 product attribute values options using console

Keeping a dropdown attribute’s options up to date in the Magento 2 admin means a lot of clicking: deleting the values you no longer sell and adding new ones, one row at a time. This script does both in one go. You give it the list of values the attribute should end up with, and it makes the Manage Options table match that list. This is an update to my previous script which was used to add custom product attribute values using the backend – Add values to product attribute ...

November 22, 2017 · 4 min · Harshvardhan Malpani

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

Magento 2.1x Cron Jobs

Magento 2 relies on cron for its background work: reindexing when indexers are set to “Update by Schedule”, applying catalog price rules, sending newsletters, generating sitemaps, updating currency rates and more. If cron isn’t set up, none of that happens, and sooner or later the admin shows a message that indexers are invalid and asks you to make sure cron is running. These are the cron entries I add for a Magento 2.1 store, either through cPanel or with crontab -e. ...

August 12, 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