magento set store id programmatically - php

I am currently working on a magento site that is in 2 languages (French and Dutch). The approach I am taking is as follows:
Create a folder in the web root (named nl)
Import the index.php and .htaccess file to that folder
In the index.php I modify the following line:
Mage::run('nl'); // to specify the store view i want to load
When I check, the categories, CMS content etc are still in the default language. The following code:
Mage::app()->getStore()->getName();
returns the fr store's name.
What is it that I'm doing wrong? I think a viable solution would be to set the store to run in index.php...
Could someone please let me know how to load a store by ID?

After hours of huffing and puffing i was able to figure out a way to set the store id programatically :)
In the index.php file, (in your language specific folder), add the following:-
$store_id = 'your_store_id_here';
$mageRunCode = 'store view code';
$mageRunType = 'store';
Mage::app()->setCurrentStore($store_id);
Mage::run($mageRunCode, $mageRunType);
Hope someone will find this information useful :)

You will get all store details here
<?php
$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val)
{
$_storeCode = Mage::app()->getStore($_eachStoreId)->getCode();
$_storeName = Mage::app()->getStore($_eachStoreId)->getName();
$_storeId = Mage::app()->getStore($_eachStoreId)->getId();
echo $_storeId;
echo $_storeCode;
echo $_storeName;
}
?>
To redirect to the specified store, you need to redirect the page along with the store code.
http://www.mywebsite.com/index.php/store_code/
Please check the template/page/switch/stores.phtml for more details

If the reason you're doing the htaccess stuff is so that you can generate URLs specific to each store, you may want to go with the configuration option that does that for you, should be in System > Config > Web

Related

CS CART -Wrong css path

I am new in cs cart.
I have done the following steps
1.downloaded a cs-cart-V3 website to my localhost.
2.Change database setting,and import database.
3.$config['http_host'] = 'localhost'; added in **config.local.php** file.
4.Removed .htaccess file from root folder.
My problem is
The site was loaded,But the path of css is wrong.It is trying to load from localhost Not from localhost/subfolder,
Where i need to change this?
Please check config.local.php
Mainly the following lines:
// Host and directory where software is installed on no-secure server
$config['http_host'] = '';
$config['http_path'] = '';
Please make sure that the store-front URL is also correct. It can be checked:
In the cscart_companies table in the database (storefront and secure_storefront columns)
On the Administration -> Stores page (Storefront URL and Secure storefront URL fields)
At the end, do not forget to clear cache. You can do it by adding the "?cc&ctpl" to the URL in the admin panel. E.g.
http://your_domain.com/your_admin.php?cc&ctpl

Multiple store view with same code

I am working on a website in magento. Which has two store (base, boutique) and 4 store view
base (fr,en) and boutique (bfr,ben).
Now when I am using store URL in site url. I am getting 4 urls for every store view
http://example.com/en
http://example.com/fr
http://example.com/boutique/ben
http://example.com/boutique/bfr
We want the url of other store as shown below :
http://example.com/boutique/en
http://example.com/boutique/fr
but we have already store view with these codes.
Please help how to solve this.
You cannot use the same code for 2 store views. Period.
But you can try a different approach to have your url's like this.
Don't use 'Add store codes to url' and instead actually create the folders you need fr, en, boutique/en, boutique/fr and copy the index.php and .htaccess files to each one of those folders and replace the following.
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
with
$mageFilename = '../app/Mage.php';
or
$mageFilename = '../../app/Mage.php';
depending on the depth of the folders.
And replace this
Mage::run($mageRunCode, $mageRunType);
with
Mage::run('store code here', 'store'); //instead of store code view put en, fr, ben or bfr
You also need to adjust the base urls for each store view from system->configuration.

Magento subshops in subdirectory

I do have 5 shops:
http://www.mainshop.com
http://www.mainshop.com/subshop/
http://www.mainshop.com/subshops/
http://www.mainshop.com/subshops3/
http://www.mainshop.com/subshop4/
But everytime i click on the url for a categroy it keeps giving a Magento 404 error.
I did copy the index.php and htaccess to that subdirs and changed this line:
/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'subshop1';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'website';
I also changed the configuration of that specific website:
The 404 comes to cms pages and category pages. Can't figure it out.
Magento creates a full links like this:
{{unsecure_base_url}}../skin/
http://www.example.com/shop/../skin/
which wont work ofcourse... Try inserting the full links ;)
Like this:
http://www.example.com/skin/
Have you used store views for each shop on one magento installation? You should not have sub directories for each shop and no need to copy index.php or any other file to a subdirectory. I think you have misunderstood how magento store views work. What you should do is enable the option for the storecode to be shown in each URL. When you setup your store view you specify the code which will be used (e.g. subdir1, subdir2 or whatever you want it to be). You need to use store views as you are using the same base URL which you need to change back to be default for each store view so it should be http://www.mainshop.com/ for every store view or website. This will allow you to achieve what you are trying to, without modding any magento code. Read this document in the wiki for more information how to do it properly http://www.magentocommerce.com/knowledge-base/entry/overview-how-multiple-websites-stores-work/

Finding path name to use as a slug for custom built cms with PHP

I'm building a website with a custom content management system, and I want to build a slug area like wordpress. I want to retrieve the path name from my front-end depending on the page they're on, and echo that out in my backend in the slug area.
I'm using php and my front-end is dynamic, which means I have one page, and depending on what the user clicks on, I will include that file.
What I want the code to look like for the slug in the backend:
<?php
//front end path/ echo $slug;
?>
My front-end path looks something like this: blahblah/index.php/slug-name
I have a slug stored in the database that I will echo out, but my problem is I don't know how to retrieve the front end path and echo it out in the backend. I realize I can type the front-end path manually, but I think doing it dynamically would be better incase I move my website to a different location in the future.
I've tried using pathinfo or $_SERVER but that echos out my backend path rather than my front end.
Hopefully I was clear, if not, ask me to clarify something. Thanks again.
You need the rewrite module for apache or nginx.
That allows you to do like this:
PrettyPath(This will be seen to all visitors): http://blah.com/blah/bl/ah/test
=> RealPath(This can be used for develop): http://blah.com/blah/index.php?slug=bl/ah/test
You can do beautiful job like this for using rewrite module. (Rewrite Example)
$front_end_path = 'your/site/path';
$full_url = $front_end_path . $slug;

Can a Directory alone load specific query on a page?

On websites such as facebook and many others you see URLs such as www.facebook.com/username. How does a URL such as this actually load the users information from a MySQL database? and what is the actual file it is displaying on? I would assume there's not really a folder for each user that it is referring to. If my question doesn't make sense can you at least point me in the right direction to set something like this up through PHP?
Again, I want example.com/username to load my users profile. How does this work?
By using apache's .htaccess file to manage a RewriteEngine, all of your pages can be funneled through an index.php file. After confirming that the requested page is not actually a page that you've intended to be a default part of your web page, you can fall back on the code below, to discover a user account. If a user account is not discovered, then the likelihood is that the page being accessed is simply a 404, which you could then redirect to as a catch-all scenario
.htaccess file
RewriteEngine on
RewriteBase /
RewriteRule !\.(xml|js|ico|gif|jpg|png|css|swf|php|txt|html|otf)$ index.php
php logic to run after confirming the requested page, isn't something like a contact-us page, or any typical web page an end user would be attempting to access.
if(preg_match("/^\/(?P<username>[^\/]*)/", $_SERVER['REDIRECT_URL'], $matches)) {
$result = mysql_query("SELECT * FROM users WHERE username = '" . mysql_real_escape_string($matches['username']) . "'");
if($user_record = mysql_fetch_row($result)) {
echo "DO WHATEVER YOUR HEART CONTENTS HERE :)";
} else {
header("Location: error-404.php");
}
}
It is all loaded dynamically via the database. For example, my Facebook name is "benroux". In facebook's user table, there is going to be a unique column called, lets say, nickname. When I visit Facebook, they are parsing the path info and essentially calling a query in the form:
select * from user where nickname = "{$nickName}"
Now, this is an over simplified example, but I hope it gives you an idea of what is going on here. The key is that there are 2 types of url vars, the facebook.com/pagename.php?id=blah and the restful style path info vars facebook.com/pagename/var1/var2/
If you are looking to have example.com/benroux load my user page, you need to make your index.php (I'll use PHP) load the path info ( http://php.net/manual/en/function.pathinfo.php ) and then query the database as I have described above.
try to
print_r($_SERVER);
you will get that parameters. Then you just need to split them.
Something like
$directory = $_SERVER['REQUEST_URI'].split('/')[1];
So, put $directory into query

Categories