Reputation: 11
Is there any php code to change store? So for example if php detects the language is Spanish I would like it to change the whole store to other store.
So it can be used for example anytime when language is changed - then the store is changed also. So for example if anyone is viewing the store1 in English and change language to Spanish then automatically the store1 is also changed to store2 toghether with the language. Any idea?
Upvotes: 0
Views: 923
Reputation: 417
If you open file: index.php or startup.php (based on the OpenCart version you use) you will see that there is a code that checks the url of which the user has visited and then searches the database to see if it's default store (store_id = 0) or other store. If it's the default store, then nothing changes. If it's not though, then session variable (store_id) is changed to the specified store_id and the whole opencart will operate based on this store_id.
OpenCart 1.5.x index.php
// Store
if (isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) {
$store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`ssl`, 'www.', '') = '" . $db->escape('https://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
} else {
$store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`url`, 'www.', '') = '" . $db->escape('http://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
}
if ($store_query->num_rows) {
$config->set('config_store_id', $store_query->row['store_id']);
} else {
$config->set('config_store_id', 0);
}
OpenCart 2.x / 3.x - catalog/controller/startup/startup.php
// Store
if ($this->request->server['HTTPS']) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`ssl`, 'www.', '') = '" . $this->db->escape('https://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`url`, 'www.', '') = '" . $this->db->escape('http://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
}
if (isset($this->request->get['store_id'])) {
$this->config->set('config_store_id', (int)$this->request->get['store_id']);
} else if ($query->num_rows) {
$this->config->set('config_store_id', $query->row['store_id']);
} else {
$this->config->set('config_store_id', 0);
}
So, you can extend this code and you will check the browser's language and based on every language you will change the store_id to the one you need.
A sample code is the following, it's not the ideal, but it will give you the idea on how to implement it by yourself.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
$this->config->set('config_store_id', 1);
break;
case "it":
$this->config->set('config_store_id', 2);
break;
case "en":
$this->config->set('config_store_id', 3);
break;
case "el":
$this->config->set('config_store_id', 4);
break;
default:
//else default store
$this->config->set('config_store_id', 0);
break;
}
Hope the above answer will help you. It's easy, but you need to understand how OpenCart works. Also, just my advice..."Don't wait for ready-made extensions, try to make yours." ;)
Cheers!
Upvotes: 2