Enabling maintenance mode in PrestaShop programmatically can be useful when you want to perform updates or maintenance tasks on your online store. To enable maintenance mode in PrestaShop using code, you can create a simple PHP script or modify specific files within your PrestaShop installation. Here are two methods to achieve this:
Method 1: Using a PHP Script
Create a PHP script in your PrestaShop root directory (where your index.php
file is located) with the following code:
<?php
include_once(‘config/config.inc.php’);
include_once(‘init.php’);
if (!defined(‘_PS_VERSION_’)) { exit; }
// Define the path to your PrestaShop config file
define(‘_PS_ROOT_DIR_’, __DIR__);
// Include the PrestaShop configuration
include(_PS_ROOT_DIR_.’/config/config.inc.php’);
// Enable maintenance mode
Configuration::updateValue(‘PS_SHOP_ENABLE’, 0);
// Display a message to inform visitors
Tools::displayMaintenancePage();
// This script should not be accessible directly in the browser, so exit here
exit;
?>
This script includes the PrestaShop configuration, updates the ‘PS_SHOP_ENABLE’ configuration value to 0 (maintenance mode enabled), and then displays the maintenance page using Tools::displayMaintenancePage()
.
To disable maintenance mode and make your store accessible again, set the ‘PS_SHOP_ENABLE’ configuration value back to 1 (enabled).
Method 2: Modifying .htaccess File
Another way to enable maintenance mode is by modifying your .htaccess
file. This method is more of a workaround but can be effective. Here’s how to do it:
- Open your PrestaShop installation directory.
- Locate the
.htaccess
file and make a backup copy. - Edit the
.htaccess
file with a text editor and add the following lines to the beginning of the file:apache
# Enable maintenance mode
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^Your_IP_Address
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^(.*)$ /maintenance.html [R=302,L]
Replace
Your_IP_Address
with your IP address. This code redirects all visitors to a ‘maintenance.html’ page except for the IP address specified. You should create a ‘maintenance.html’ file with a maintenance message in your PrestaShop root directory.- Save the changes to the
.htaccess
file. - When you’re done with maintenance, remove the lines you added to the
.htaccess
file to disable maintenance mode.
Choose the method that best suits your needs, and make sure to have a backup of your PrestaShop files and database before making any changes. It’s also a good practice to notify your customers in advance when enabling maintenance mode to avoid any inconvenience.