Magento 2 : How to fix tablespace for table exists | base table or view already exists

Fixing General error: 1813 Tablespace for table xx Please DISCARD the tablespace before IMPORT and SQLSTATE[42S01]: Base table or view already exists\ Recently I encountered this error while upgrading an extension on Magento 2.2.2 The error was SQLSTATE[HY000]: General error: 1813 Tablespace for table '`jack_db1`.`jill_table`' exists. Please DISCARD the tablespace before IMPORT., query was: CREATE TABLE IF NOT EXISTS `jill_table` When I tried to fix the above error, I received: ...

February 26, 2018 · 2 min · Harshvardhan Malpani

Remove sku column from Magento 2 invoice and pdf

Magento 2’s invoice PDFs have a SKU column next to each product, and order emails print the SKU under each product name. If you’d rather not show your SKUs to customers, there’s no setting to turn them off, so this is how I removed them: by editing the code that draws the PDF and by overriding the email template in the theme. Edit the file /vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php Inside function draw(), remove/comment following code: ...

February 21, 2018 · 3 min · Harshvardhan Malpani

Magento 2 temporary fix for Alphabetical sorting of Configurable product options

On a configurable product, such as a T-shirt that comes in several sizes and colours, Magento 2 doesn’t always list the options in the dropdowns or swatches in the order you want. This is the quick fix I used to show them in alphabetical order. It’s a core hack, which is why the title calls it temporary; see the notes at the end before using it. Just re-write the file vendor/magento/module-configurable-product/Model/ConfigurableAttributeData.php with the following, then flush the cache with php bin/magento cache:flush so product pages are rebuilt with the new order. ...

February 16, 2018 · 3 min · Harshvardhan Malpani

How to add custom attributes in Magento 2 product CSV export

When you export products in Magento 2 (System > Data Transfer > Export, with Products as the entity type), the standard attributes such as sku, name and price each get a column of their own. Your custom attributes don’t. They are packed together into a single additional_attributes column as code=value pairs, which is awkward to sort or filter in a spreadsheet. The list of attributes that get their own column is hard-coded in the export model, so the quick way to add yours is to extend that list. ...

January 25, 2018 · 2 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

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

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