1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact

Magento Tutorials

Archive for the ‘Magento Tutorials’ Category

Subcategories Within Categories

Posted on: 19th Jul 2010 By: Adam Moss 1 Comment

I did a tutorial ages ago about how to import the subcategories of a product within another category via a static block. The details were shady and the logic was sketchy in those days, now I can look upon it with a sense of absolute knowledge and appreciation. Before digressing uncontrollably, let me get straight to the point.

Subcategories

1. Create a page called subcategory.phtml in the following folder: app/design/frontend/base/default/template/catalog/navigation/ and use the following code:

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
<?php endif; ?>
<?php endforeach; ?>

This is the code represented as simply as possible, it will produce a list of links that are contained within that category, you’re free to style this as you wish.

2. Create a static block called Subcategories and enter the following code into the content:

{{block type="catalog/navigation"  template="catalog/navigation/subcategory.phtml"}}

3. Now go to the category which you’d like to use to show the subcategories and ensure that it is not set as an anchor. In the Display options tab, set it to show as ‘Static Block Only’ and choose the block ‘Subcategories’.

Add Subcategory Image

If you want to take it one step further and show the image of the subcategory to make it more visual, you need to make a simple hack and add a bit of code to the original code in subcategory.phtml.

1. Firstly open app\code\core\Mage\Catalog\Block\Navigation.php, copy all the contents and create a new file locally. So that would be: app\code\local\Mage\Catalog\Block\Navigation.php

2. Look for a function on line 93 called getCurrentChildCategories() and comment out the function, replacing it with the following:

$layer = Mage::getSingleton('catalog/layer');
$category   = $layer->getCurrentCategory();
/* @var $category Mage_Catalog_Model_Category */
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToSelect('image')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();

$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
return $collection;

Save and upload this file. When a local file is uploaded, it is used instead of the core file.

3. Enter the following code into the subcategory.phtml file that we created in the first part of the tutorial. Make sure it occurs within the foreach statement and inside the getIsActive() check:

<img src="<?php echo $_category->getImageUrl() ?>" width="90" height="90" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />

You can now visually display your category’s subcategories in a very simple way. Keep checking back for more of our Magento Tutorials here at Ecommerce Web Design.

Categories: Magento Tutorials Tags:

One Magento Installation on Multiple Domains

Posted on: 12th Jul 2010 By: Adam Moss 6 Comments

Plesk is a terrific control panel for multiple reasons – it allows complete administration for all of your websites that are hosted there. This includes the setup of SSL certificates, local database access, SSH access, the ability to switch domains off at the flick of a switch and much more. By much more I’m talking about ‘Domain Aliases’.

It has become an increasing requirement for me to have multiple domains  using the same Magento installation, and those multiple domains show different websites that are set up on that installation. I have domain1.co.uk which holds the Magento Installation, I also have domain2.co.uk which is a website on that Magento Installation. Okay so here’s how you do it…

1. Point DNS – Point the DNS A Record of domain2.co.uk to the IP address of domain1.co.uk.

2. Create Domain Alias – In Plesk, go to the domain overview area for your version of domain1.co.uk and click on ‘Domain Aliases’. Now click ‘Add Domain Alias’ and type in the name of the website (without the http://www part). So I type in domain2.co.uk.

Domain Alias

3. Create Website in Magento - In the Magento Admin area go to System > Manage Stores. Click on ‘Create Website’ and enter the name of the domain into Name field. You also need to enter a code to identify the website (you’ll need this later) – I just went with domain_2. As this isn’t my4.  primary domain, I put the sort order as 2. You should also create an associative store and store view for this new website in the same way. Read my earlier post about websites/stores/store views for more information.

New Website

4. Change the Base URL - Go to System > Configuration > Web. Change the configuration scope (top left) to the secondary website (domain2.co.uk). You now need to change the secure and unsecure Base URL to the new domain, without forgetting to put the forward slash at the end of both.

Base URL

5. Update the .htaccess – Now that everything’s in place it’s simply a case of adding some code to the bottom of your main Magento .htaccess file so that Magento knows to load your new website when accessed from that domain. The code for my website domain2.co.uk would look like this:

SetEnvIf Host www\.domain2\.co.uk MAGE_RUN_CODE=domain_2
SetEnvIf Host www\.domain2\.co.uk MAGE_RUN_TYPE=website
SetEnvIf Host ^domain2\.co.uk MAGE_RUN_CODE=domain_2
SetEnvIf Host ^domain2\.co.uk MAGE_RUN_TYPE=website

You can see at the end of line 1 & line 3 that the website code I defined earlier as ‘domain_2′ has been appended accordingly. Save your .htaccess file and upload (or just save it if you’re doing it through Plesk). Without sounding like the official advocate of all that is Plesk, it really is a good way of configuring the server settings of your Magento site and can save you a lot of time and energy.

Thanks for reading the Magento Blog at E-commerce Web Design, I’d love to know how you all get on with creating your additional Magento URLs so please leave a comment and let me know.

Categories: Magento Tutorials Tags:

Delete Stores / Websites in Magento

Posted on: 6th Jul 2010 By: Adam Moss 2 Comments

In a recent project I had to remove websites, stores and store views in Magento because I simply didn’t need them any more. I couldn’t wait to get into that admin area and clear it all up. It’ll be nice and simple, I’ll just open the store view and look for the nice red ‘Remove Store View’ button in the top right corner…. *gasp*

Several hours followed my collapse, luckily I only suffered minor concussion from the fall. However when I came around the cold dark truth of what I was hoping was a nightmare became very real indeed. There is no way of removing stores that you create in Magento.

Luckily it’s actually very easy to remove these stores/websites/store views in the database – phpMyAdmin gives you the ability to delete things! I guess you could use any other database administration program too. There are 3 tables to look for:

  1. core_website to remove websites
  2. core_store to remove store views
  3. core_store_group to remove stores

phpMyAdmin

I should mention that you should NOT DELETE the first row of each of these tables. Now I’d like to ask Magento to include the delete button in the next release to free us from this madness.

Categories: Magento Tutorials Tags:

Customise Navigation Menu Links

Posted on: 18th Jun 2010 By: Robert Kent 1 Comment

The navigation menu in magento is more versatile than it seems. Obviously the menu does what it needs to do – list categories and subcategories in their respective positions. However recently I got a request to manipulate the menu and make some things click-able and others not click-able.

The client wanted only the landing pages to be links – this meant that any list item with a drop down list appearing off of it would not be click-able – however all the ending pages – such as the subcategories had to be linked. Another interesting one was that they still wanted the main categories to be links – even though they have drop downs coming off them…strange huh?

I’m not quite sure what the purpose of this exercise was but apparently it made more sense to their e-commerce web design if the menu acted this way, however bad it may seem for SEO purposes.

I thought that this could be useful for some people so I have decided to share it here on our magento blog. The magento fox may even tweet about this too.

First of all I found the core code that needs editing – the drawItem() function. You can find this in the app>code>core>Mage>Catalog>Block>Navigation.php.

As always you should make a local copy of this file so that when upgrading magento your changes are not undone.

In Navigation.php you will find the function drawItem() all you have to do is replace the part that includes the <anchor tag> with a version with no <anchor tag>. The tricky part is determining which items get the links and which do not.

Find this line in your code:

$html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";

And replace with:

if($hasChildren && $level != 0){
$html.= '<span>'.$this->htmlEscape($category->getName()).'</span>'."\n";
} else {
$html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
}

This should make immediate sense to any magento user – basically if the level is not 0 (i.e NOT the main categories) and also if it has children (has drop downs coming off) then don’t add an anchor tag around the text. Something that I thought would be really hard to achieve was actually really easy and completed in 5 minutes.

If you found this blog useful please leave a comment and let me know, I’d love to hear the situations where you would need to use this code because at the moment, just like that darn magento fox, it’s eluding me.

Show Magento Products by Attribute

Posted on: 17th Jun 2010 By: Adam Moss No Comments

You may remember Rob’s recent post about Smart Product Lists which detailed how to bypass Magento’s usual database interactions, and get straight in there yourself with your own MySQL code. Putting this to good use the other day I developed this script which does something so simple, yet can be quite difficult if you don’t know where to start. Trust me, I checked the Internet and there’s nothing out there which helps you do this.

What I needed to was print out all the products in the store which contained the words ‘Full Colour Printing’ in the attribute ‘pen_feature’. So, if anywhere in the string the words ‘Full Colour Printing’ appear, my script will tell the product name along with the product link to appear on the CMS page that I implemented it on. I’ll go through the code step by step:

1. First we need to inlcude the Magento Database Connection.

require_once 'app/Mage.php';
Mage::app('default');

2. Next we need to create an SQL statement and run a query which would effective grab every product ID contained in the store and store it in $result. In my case that was over 300 products. The ORDER BY bit isn’t necessary as it will probably put them in order anyway!

$write =  Mage::getSingleton('core/resource')->getConnection('core_write');
$result  = $write->query("SELECT product_id FROM catalog_category_product  ORDER BY product_id");

3. Now I’m going to write a while loop which will only execute the rest of the script while the $result can retrieve database data. Each row is now deliverers as a string, which means you’ll have a long list of numbers in one variable… not very helpful. So, I’m going to create a new variable, $prodid, which will become an array of all the product IDs by simply exploding the numbers at each blank space. Bingo – we now have a beautiful array of product IDs.

while ($row = $result->fetch() ) {

$prodid = explode(" ",  $row['product_id']);

4. Now for a bit of Magento code. I’m gonna start with a foreach statement which will convert $prodid into $id, then I’m going to load the Mage product data into each loop, each time the product being loaded by its ID.

foreach ($prodid as $id) {

$_product = new  Mage_Catalog_Model_Product();
$_product->load($id);

5. Now, I need to narrow down all these products so that it only brings out the ones with ‘Full Colour Printing’ within the pen_features attribute. So I load the attribute as $feature, and I create a variable for ‘Full Colour Printing’. Now I can use strpos with $feature as the haystack and $fullcol as the needle. With that check in place, one more if statement around the echoed script will check if the $fullcolcheck is true or not.

$fullcol =  "Full Colour Printing";
$feature =   $_product->getResource()->getAttribute('pen_features')->getFrontend()->getValue($_product);

$fullcolcheck = strpos($feature, $fullcol);

if ($fullcolcheck !==  false) { ?>

6. Now it’s simply a case of bringing out the product data, remember we’re still inside that foreach and while loop! You could bring out much more such as the price or image too.

<li><a href="<?php echo $_product->getProductUrl()  ?>"><?php echo $_product->getName();  ?></a></li>

That’s really all there is too it – not bad eh? You can probably simplify this script even more as it’s unlikely you’ll need to use the strpos if you’re checking attributes. I’ve put the entire script below for you to use as you wish. Keep checking back at the Magento Blog at Ecommerce Web Design for more of our Magento tutorials.

<ul><?php

require_once 'app/Mage.php';
Mage::app('default');

$write =   Mage::getSingleton('core/resource')->getConnection('core_write');
$result  = $write->query("SELECT product_id FROM  catalog_category_product  ORDER BY product_id");

while ($row = $result->fetch() ) {

$prodid = explode(" ", $row['product_id']);

foreach ($prodid as $id) {

$_product = new Mage_Catalog_Model_Product();
$_product->load($id);
$fullcol = "Full Colour Printing";
$feature = $_product->getResource()->getAttribute('pen_features')->getFrontend()->getValue($_product);

$fullcolcheck = strpos($feature, $fullcol);

if ($fullcolcheck !== false) { ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName(); ?></a></li>
<?php } } } ?></ul>
Categories: Magento Tutorials Tags: