Importing Pages and Blocks in Magento

Published by John on May 7, 2018 Under Magento

Magento LogoI’ve been working on a site makeover for one of my clients currently running a pre 2.X Magento Installation.

We’ve not done too much with their main website design, but have added a number of new landing pages, as well made changes to product attributes, CMS Blocks, some mobile/responsive fixes, etc.

The biggest updates, however, were to the landing pages we created and this site uses a custom Magento Plugin I wrote for adding additional fields to CMS Pages, like slides, related products, testimonials, and other cool features. This has let us create some really cool and dynamic landing pages, without having to just add custom CSS/HTML to the WYSIWYG Editor, so they deviate a lot from your normal boring Magento CMS Page.

All of this work has been done on a development website and unlike when you launch a WordPress website, where you can often just replace the live Mysql Database with the Development Database, this is not generally possible with an active Magento Installation, as the Magento store will have products, customers, orders, and tons of other data that you don’t want to loose.

So, the changes need to be merged into the website database, rather than replacing everything.

While we have a ton of product and category updates too, I have written an import script to handle that, so the main issue was merging in the CMS Pages and CMS Blocks. After some trial and error, I found the easiest and quickest way to do this is to just drop the cms_page, cms_page_store, cms_block, and cms_block_store tables and then import them from the development database. I tried a few other methods, like using REPLACE INTO instead of INSERT, but ran into issues with the cms_pages not syncing up afterwards.

However, it is important to note that this will clear out all of the current pages/blocks on the live website, so if you need to save any of them, you will need to take additional steps!

Please make sure to read the disclaimer below before doing anything!

How to Quickly Import CMS Pages and Blocks in Magento 1.X

Disclaimer: Backup both databases first! The below will delete ALL of your pages/blocks in one database and replace them with the new ones. So, only do the below if you are fine with that or are prepared to add back the other pages/blocks some how!

No really, backup your databases and don’t try the below if you are not comfortable restoring from a mysql database!

Definitions(replace the below with your actual values:)

dev_database – The development database that contains the pages/blocks that you want to use.
dev_database_user – A mysql user with access to dev_database.

live_database – The old database that you want to replace all pages/blocks with those from the dev_database.
live_database_user – A mysql user with access to live_database.

Affected Tables: cms_page_store, cms_page, cms_block_store, cms_block

Steps:

1) Export the affected tables from the development database:


[BASH ~]$ mysqldump  -u dev_database_user -p dev_database cms_page cms_page_store > cms_page_import_2018-05-07.sql
[BASH ~]$ mysqldump  -u dev_database_user -p dev_database cms_block cms_block_store > cms_page_import_2018-05-07.sql

2) On the live database, drop the tables. Make sure to do it in this order:


[BASH ~]$ mysql -u live_database_user -p live_database

mysql> drop table cms_block_store;
mysql> drop table cms_block;
mysql> drop table cms_page_store;
mysql> drop table cms_page;

3) Import the cms_page/cms_block files created in step #1


[BASH ~]$ mysql  -u live_database_user -p live_database < ~/cms_page_import_2018-05-07.sql
[BASH ~]$ mysql  -u live_database_user -p live_database < ~/cms_page_import_2018-05-07.sql

4) If you had any pages/blocks you need to re-create, do so now on the live website. Also, check places that might be using the Block ID, like if you are using a block on a category, as it is possible this may have changed if you have been adding blocks on both the live and development website as you’ve been working.


No Comments |

Add a Comment