Move website from one domain to another domain in opencart

Here is the steps to migrate opencart website from one server to another server.

  1. Download ALL the files from old server.
  2. Take a backup of Database using PhpMyAdmin.
  3. Upload files to NEW SERVER.
  4. Install Database on NEW SERVER
  5. Update the configuration files i.e config.php and admin/config.php with new domain path.

Update config file with following details.

<pre>
// HTTP
 
define('HTTP_SERVER', 'http://yournewdomain.com/');
 
// HTTPS
 
define('HTTPS_SERVER', 'http://yournewdomain.com/');
 
// DIR
 
define('DIR_APPLICATION','/root-folder-newdomain/public_html/catalog/');
define('DIR_SYSTEM','/root-folder-newdomain/public_html/system/');
define('DIR_DATABASE','/root-folder-newdomain/public_html/system/database/');
define('DIR_LANGUAGE','/root-folder-newdomain/public_html/catalog/language/');
define('DIR_TEMPLATE','/root-folder-newdomain/public_html/catalog/view/theme/');
define('DIR_CONFIG','/root-folder-newdomain/public_html/system/config/');
define('DIR_IMAGE','/root-folder-newdomain/public_html/image/');
define('DIR_CACHE','/root-folder-newdomain/public_html/system/cache/');
define('DIR_DOWNLOAD','/root-folder-newdomain/public_html/download/');
define('DIR_LOGS','/root-folder-newdomain/public_html/system/logs/');

// DB
 
define('DB_DRIVER','mysql');
define('DB_HOSTNAME','localhost');
define('DB_USERNAME','your-new-database-username');
define('DB_PASSWORD','your-new-database-password');
define('DB_DATABASE','your-new-database');
define('DB_PREFIX','oc_');

</pre>


Save the changes and upload files on new server.

Direct SQL Queries In Magento

Magento uses data models which provide a great way to access and modify data. By using competently named methods and clever abstraction, Varien hide away the complex SQL needed to perform data operations. While this makes learning models easier, it often impacts the speed of the operation and so the responsiveness of your website. This is true when saving models that use the EAV architecture.

Mostly, this cannot be avoided, however there are some situations where executing direct SQL queries would be simpler and much quicker leading to a more optimised Magento installation.

Example : Product price update globally in magento.

 

Database Connections In Magento

Magento will automatically connect to it’s database and provide two separate resources which you can use to access data: core_read and core_write.

<?php 
/** 
* Get the resource model 
*/ 

$resource = Mage::getSingleton('core/resource'); 

/** 
* Retrieve the read connection 
*/ 

$readConnection = $resource->getConnection('core_read');
	
/**
* Retrieve the write connection
*/
$writeConnection = $resource->getConnection('core_write');

?>

 

Table names and prefixes

A table prefix is a string of characters that is added to the start of every table name in your database. These are useful to installing multiple system into one database. It helps to distinguish each application’s data.

Magento has a simple built in function which allows you to add the prefix to a given table name.

Get a table name from a string
<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Get the table name
*/

$tableName = $resource->getTableName('catalog_product_entity');
	
/**
* if prefix was 'mage_' then the below statement
* would print out mage_catalog_product_entity
*/

echo $tableName;

?>

 

Get a table name from a string
<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Get the table name
*/

$tableName = $resource->getTableName('catalog/product');
	
/**
* if prefix was 'mage_' then the below statement
* would print out mage_catalog_product_entity
*/

echo $tableName;
?>

 

Reading From The Database

It would be much quicker to run a single query to obtain this value than to load in a whole product model .

Varien_Db_Select::fetchAll

This method takes a query as it’s parameter, executes it and then returns all of the results as an array.

<?php
	
/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');
	
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
	
/**
* Execute the query and store the results in $results
*/

$results = $readConnection->fetchAll($query);
	
/**
* Print out the results
*/

var_dump($results);

 

Varien_Db_Select::fetchCol

This method is similar to fetchAll except that instead of returning all of the results, it returns the first column from each result row.

<?php
/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');
	
/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');

/**
* Execute the query and store the results in $results
*/

$sku = $readConnection->fetchCol('SELECT sku FROM ' . $table . ');
	
/**
* Print out the results
*/

var_dump($results);

 

Varien_Db_Select::fetchOne

Varien_Db_Select::fetchOne returns one value from the first row only.

<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');

/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');
	
/**
* Set the product ID
*/

$productId = 44;
	
$query = 'SELECT sku FROM ' . $table . ' WHERE entity_id = '. (int)$productId . ' LIMIT 1';
	
/**
* Execute the query and store the result in $sku
*/

$sku = $readConnection->fetchOne($query);
	
/**
* Print the SKU to the screen
*/

echo 'SKU: ' . $sku . '<br/>';

 

Writing To The Database

<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the write connection
*/

$writeConnection = $resource->getConnection('core_write');

/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');
	
/**
* Set the product ID
*/

$productId = 44;
	
/**
* Set the new SKU
* It is assumed that you are hard coding the new SKU in
* If the input is not dynamic, consider using the
* Varien_Db_Select object to insert data
*/

$newSku = 'new-sku';
	
$query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = ". (int)$productId;
	
/**
* Execute the query
*/

$writeConnection->query($query);