Magento change store via url

In a multi store environment there is sometimes a need to change the store when the user clicks a link.
A “simple” way to do this is to pass the store along in the url (just add a “?___store=” either id or the store name, so for instance.

$stores = $category->getStoreIds();
echo '<p><a href="'. Mage::getUrl() . '?___store=' . end($stores). '">'.$category->getName()."</a></p>";

Magento: Get the skin url

When editing Magento tempates or CMS pages you will have a need to access images and other content inside the skin/—/images folder. Instead of making hard coded links in the CMS or templates a better way is to get this from Magento (one of the many reasons for doing this is that it will still work if something in the directory structure is changed – like say you dev server is dev.something/shopverige but the live store is directly on a domain like say http://www.shopsverige.se

Here is how to do it:

<!-- this is inside of .phtml files -->
<img src="<?php echo $this->getSkinUrl('images/coolimage.png'); ?>" alt="arrow"/>
 
<!-- use this is in cms block -->
<img src="{{skin url='images/coolerimage.png'}}" alt="" />

Magento Selling clothing (or anyother product with options) while having Magento keep track of stock

Creating confugurable products is more work than adding custom attributes but it might be better in some cases (keeping track of stock, having one set of attributes to select from etc).

In short the steps needed are

  1. Create the attributes that will be configurable by the user – for our example they will be Size and Color
  2. Create the attribute set that will be assigned to the variant products – for our example, we’ll call it “T-shirt”
  3. Create the individual variant products
  4. Create the configurable product, and add the “T-shirt” attribute set
  5. Add the individual variants to this configurable product

Create the attributes that will be configurable by the user – for our example they will be Size and Color
In the adminpanel this is under Catalog->Attributes->Manage Attributes
Scope needs to be set to Global and Input type to drop down. Once this is done then Use To Create Configurable Product is shown and this should be set to yes. Setting required to yes is a good idea as well.

Under label / options create a label (for instance size) and give the different options it can take.

Repeat for all attributes you want to create (for instance size and color)

Create the attribute set that will be assigned to the variant products – for our example, we’ll call it “T-shirt”
Now we’re ready to create an attribute set called “T-shirt” to start using for this product. Go to “Catalog -> Attributes -> Manage Attribute Sets” and press “Add New Set”.
Give the new set a name and then drag and drop the attributes to the group where you want them to be when creating a product.

Create the individual variant products
Create a product and make sure that you use the attribute set you have created. Add the attributes where you placed them.


Repeat for all the products you have

Create the configurable product, and add the “T-shirt” attribute set

Add the individual variants to this configurable product
This can be done when the configurable product is created or later on using edit product.

And then it should look something like this in the shop

Magento Selling clothing (or another product with options) without having Magento keep track of stock

Some products have a number of options that are important for the customer to select.
Here I show how to make a product with options (color and size) where Magento only sees one product – so it won’t it will not be possible to see how many large in red you have from Magento.

When you create a product in the admin panel there is a label Custom Options.


Use “Add new option” to make the option (color, size or whatnot)
Then for each of the options use add new row to add the deferent values that the option can have.
The column labeled price is for adding a extra cost for a option (if for instance the red shirt should be more expensive).

Then this will look something like this in the store

Then only down sides I have seen with this is that I have to add the options one time for each of the products (but that is also nice since not all products have the same values) and that Magento will treat this as one single product so I can’t let Magento manage the stock for this/these products.

Magento redirect

Sometimes there is a need to do a redirect from inside a Magento page where you might not know if headers are sent or not – Or maybe you wish to make a new redirect function where you don’t have to worry about writing different code if the headers are sent or not.

This is a way to do a redirect where a javascript redirect is used if headers have been sent (and thus it is no longer possible to use a header redirect)

if (!headers_sent()) {
 
	Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl() . 'portal/');
 
}
 
else {
 
	$baseUrl = Mage::getBaseUrl() . 'portal/';
 
	print '<script type="text/javascript">';
 
	print "window.location.href = '{$baseUrl}';";
 
	print '</script>';
 
}

And the function headers_sent() is a php function so that can of course be used outside of Magento in any php scrip where a header location is nice but not always possible.

Magento: Problem uploading images

When I was truing to upload images to products in the Magento admin panel I got the following error:
SSL Error: Invalid or self-signed certificate while uploading image from backend

The way I solved this issue was by setting the timezone on the computer that I was uploading from to the same timezone as the settings in Magento.

Other suggestions to try out if that don’t work:

  • If you are using certificate: Verify that it is valid, you have correct domains etc
  • Set use secure url in admin to false

Magento: Redirecting from inside a view

To redirect before headers are started to be sent (for instance from inside a controller) the following works:

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('portal'));

This will redirect to the page called portal.

However if you try to do this from a view then you will get an error message about headers already having been sent.
If you need to do a redirect form inside a view then using javascript is one way to do this:

echo 'Due to SOMEREASON you are being redirected <a herf="' . Mage::getUrl('portal') . '">Please click here if nothing happens</a>';
		echo '<script type="text/javascript">
		<!--
		window.location = "' . Mage::getUrl('portal') . '"
		//-->
		</script>';

However a tip is to reconsider if you really need to do the redirect from the view, doing it from the controller is a lot easier and quicker.

Magento: Loading a store by store_id

Sometimes there is a need to load a store from the store id, this is not hard to do once you find the right function.

Mage::getModel('core/store')->load($storeId); //a store object

A store object might look like this. Each of the data has a get and a set fucntion (so getIsActive() returns if the store is active or not)

 
 
Mage_Core_Model_Store Object
(
    [_cacheTag:protected] => 1
    [_eventPrefix:protected] => store
    [_eventObject:protected] => store
    [_priceFilter:protected] => 
    [_website:protected] => 
    [_group:protected] => 
    [_configCache:protected] => 
    [_configCacheBaseNodes:protected] => Array
        (
            [0] => catalog/price/scope
            [1] => web/secure/base_url
            [2] => web/secure/use_in_adminhtml
            [3] => web/secure/use_in_frontend
            [4] => web/url/use_store
            [5] => web/unsecure/base_url
            [6] => web/seo/use_rewrites
            [7] => web/unsecure/base_link_url
            [8] => web/secure/base_link_url
            [9] => general/locale/code
        )
 
    [_dirCache:protected] => Array()
 
    [_urlCache:protected] => Array()
 
    [_baseUrlCache:protected] => Array()
 
    [_session:protected] => 
    [_isAdminSecure:protected] => 
    [_isFrontSecure:protected] => 
    [_isReadOnly:private] => 
    [_resourceName:protected] => core/store
    [_resource:protected] => 
    [_resourceCollectionName:protected] => core/store_collection
    [_dataSaveAllowed:protected] => 1
    [_data:protected] => Array
        (
            [store_id] => 64
            [code] => mystore
            [website_id] => 1
            [group_id] => 25
            [name] => mystore
            [sort_order] => 0
            [is_active] => 1
        )
 
    [_origData:protected] => Array
        (
            [store_id] => 64
            [code] => mystore
            [website_id] => 1
            [group_id] => 25
            [name] => mystore
            [sort_order] => 0
            [is_active] => 1
        )
 
    [_idFieldName:protected] => 
    [_isDeleted:protected] => 
)

An an example code to find if the store with id 5 is active or not

if (Mage::getModel('core/store')->load($storeId)->getIsActive())
    //do something if we are active
else
    //do something else if the store is not active

Creating sitemap(s) in magento

A sitemap is a xml file that is used by (for instance) search engines to get a better overview of your site. It gives details about what pages exists and how ranks the pages towards themselves.
Despite the name the sitemap is not Google specific, it can be used for any search engine, or any other reason that you need a sitemap.

Create a folder for the site map and make it writable

First figure out where to place the sitemap(s) and make that location writable by the webserver.
In a Linux environment this is done from the console this is done like:

mkdir sitemap
sudo chown admin:www-data sitemap
sudo chmod 775 sitemap
touch sitemap/index.php

The touch index.php will create a empty index file (that keeps the folder from being listed.

Define a sitemap

In the admin panel go to “Catalog->Goolge Sitemap->Add sitemap”

Fill out this form with values that makes sense to you.
Note: If you have sveral stores one website, then all product that are visible on that website is added to the sitemap.

Make the site map update automatically

In the admin pangel go to “System->Configuration->Google sitemap”

As always, change to values that makes sense to you

Another (older) way of doing this is described by the guys at inchoo

Magento: Admin login redrecting to old url after a url change (even though the database was updated)

If you have the problem that you have moved a magento database (and perhaps files to) from one server to another, you have update
web/unsecure/base_url & web/secure/base_url in the database to point at the new address, but the admin panel login is still redirecting to the old address, then the solution is fairly simple.

Make the var folder writable to the web server and clear out the var/session and var/cache folders.