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

Forcing website to https and www using htaccess on apache servers

Once a site has an SSL certificate, you usually want every visitor to end up on one address, for example https://www.example.com, whether they typed http://, left out the www, or both. On Apache you can do this in the site’s .htaccess file with mod_rewrite. These rules use two separate steps: first switch to HTTPS, then add the www. A common mistake is to add the www in the HTTPS rule as well. To first force HTTPS, you must check the correct environment variable, %{HTTPS} off. If that rule then also prepends www., a visitor who already typed www ends up on www.www.. Since you have a second rule to enforce www., don’t use it in the first rule. ...

February 21, 2018 · 3 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 how to show configurable options stock availability on product page

On a Magento 2 product page the quantity is a plain number box, and shoppers only find out there isn’t enough stock after they click Add to Cart. I wanted a quantity dropdown that only offers what’s actually in stock. For a configurable product the list has to change when the shopper picks an option, because each option is a separate child product with its own stock. That takes two changes in your theme: the add-to-cart template, which prints the stock quantities into the page, and the JavaScript that handles the configurable options, which rebuilds the dropdown whenever an option is picked. Both files below are complete copies of the core files as they were in February 2018, with my changes in them. ...

February 16, 2018 · 13 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

Show Available Stock quantity for each product in Magento 2

Magento 2 only tells customers whether a product is in stock, or shows “Only X left” when stock falls below a threshold. Sometimes a store wants to show the actual quantity available, on the product page or in the product list. This snippet prints the stock quantity of a product from inside a template. <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface'); echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId()); ?> How it works \Magento\Framework\App\ObjectManager::getInstance() gets Magento’s object manager, which can create any class. $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface') fetches the stock service from Magento’s CatalogInventory module. getStockQty($product->getId(), $product->getStore()->getWebsiteId()) returns the quantity for that product, given its ID and the website it’s being shown on, and echo prints it. Using it Put it in the template where you want the number to appear, for example a copy of the product page or product list template in your own theme, then clear the cache with php bin/magento cache:clean. It expects a variable called $product holding the product. Check what the template you’re editing calls it: in the category product list template the loop variable is $_product, and on the product page you can get the product with $block->getProduct(). Change $product in the snippet to match. ...

February 9, 2018 · 2 min · Harshvardhan Malpani

Compressing images directly on web server – Better Pagespeed tips

Large images are one of the most common things PageSpeed Insights complains about. If a site already has hundreds of PNG files on the server, downloading them, compressing them on your computer and uploading them again is slow. optipng can do it directly on the server over SSH. It’s lossless: it recompresses the PNG data more efficiently without changing a single pixel, so the images look exactly the same, only smaller. ...

February 2, 2018 · 3 min · Harshvardhan Malpani

How to update Magento community edition using CLI

This is the sequence of commands I use to update a Composer-based Magento 2 Community Edition store to a newer release over SSH. It was written for Magento 2.2, and the example moves to 2.2.6. Replace 2.2.6 with the version you want. Before you start Back up the database and the files. setup:upgrade changes the database and there’s no undo, and the “hard fix” further down deletes vendor/ and var/ completely. Run everything from the Magento root, the folder that contains composer.json and bin/magento, as the user that owns the Magento files rather than root. Put the store in maintenance mode with php bin/magento maintenance:enable, so visitors don’t see a half-updated site. Turn it off with php bin/magento maintenance:disable at the end. Stay within one release line. These steps are for moves like 2.2.5 to 2.2.6. Jumping to a new minor version such as 2.3 usually needs more changes in composer.json, and sometimes a newer PHP version, so read the release notes first. The update composer require magento/product-community-edition 2.2.6 --no-update composer update rm -rf var/di var/generation var/cache var/view_preprocessed generated/* php bin/magento cache:clean php bin/magento cache:flush php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento indexer:reindex php bin/magento deploy:mode:set production composer require magento/product-community-edition 2.2.6 --no-update changes the required Magento version in composer.json without downloading anything yet. composer update downloads the new version and its dependencies. Note that it updates every package composer.json allows, not only Magento, so third-party extensions can move to newer versions too. rm -rf var/di var/generation var/cache var/view_preprocessed generated/* removes generated code, cache and preprocessed theme files left over from the old version. Magento 2.1 kept generated code in var/di and var/generation; 2.2 moved it to generated/. Listing both covers a store that is coming from 2.1. php bin/magento cache:clean and php bin/magento cache:flush empty Magento’s cache. cache:clean removes Magento’s own entries, cache:flush clears the whole cache storage. php bin/magento setup:upgrade updates the database schema and data to match the new module versions. php bin/magento setup:di:compile generates the dependency injection configuration and the generated classes again. php bin/magento indexer:reindex rebuilds the indexes. php bin/magento deploy:mode:set production switches the store to production mode. While doing that, Magento compiles the code and deploys the static files (CSS, JavaScript, images) itself, which is why there’s no separate setup:static-content:deploy in this list. When it finishes, check the storefront and the admin. ...

February 1, 2018 · 4 min · Harshvardhan Malpani

How to shift category description below products in Magento 2

By default, Magento 2 shows a category’s description above the product list. That’s a problem when the description is a long block of SEO text: visitors have to scroll past it before they see any products. Moving it below the products keeps the text on the page for search engines while the products come first. You can do this with a small layout file in your theme, without changing any templates. ...

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