Tag Archives: catalog

Tips For Creating Dynamic Category Landing Pages

This is quite a common want, so I thought I would put together a quick tutorial with some ideas and pointers. My main goal will be to give you a starting point for building a static block and PHTML file that can be applied to top level categories to dynamically create a block with of all the subcategories.

The first thing to do is create a new static block (CMS → Static Blocks), lets call it ‘Dynamic Landing Pages’. Within the content paste this:

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}

If you’ve not seen one of these before, then basically it loads the list.phtml into the block allowing you to include dynamic content in your site.

Next create list.phtml in app/design/frontend/default/your_theme/template/catalog/category/

Then go to ‘Manage Categories’ select the relevant category and then choose ‘Static Block Only’ in the ‘Display Mode’ dropdown and then choose the ‘Dynamic Landing Pages’ static block we’ve just created from the ‘CMS Block’ dropdown.

Now to look at some coding to put in list.phtml, to get the category id of the current category and an array containing the ids of its child categories, use the following:

?Download list.phtml
1
2
3
4
$current = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load((int)$current);
$children = $category->getChildren();
$children = explode(",",$children);

This leaves you with:
$current, your current category id.
$category, which is an instance of Mage_Catalog_Model_Category.
$children, is an array of the child category ids.

Then cycle through each of the child categories by loading the category object using the id from the array. We check first that the first value is not just an empty string (as it will be if there are no categories):

?Download list.phtml
5
6
7
8
9
10
11
12
if (strlen($children[0]) > 0)
{
	foreach($children as $child)
	{
		$_child = Mage::getModel('catalog/category')->load($child);
			// then use the $_child object to pull out category properties
	}
}

Some of the key methods that I then subsequently used to create the landing pages were:

  • $_child->getName()
  • $_child->getUrl()
  • $_child->getImageUrl()
  • $_child->getProductCount()

The Image URL is based on the image that is uploaded in the ‘General Information’ tab in ‘Manage Categories’ but be aware that if you use this functionality you will probably want to edit frontend/default/your_theme/template/catalog/category/view.phtml so that it doesn’t load that image at the top of the subcategory.

To view the methods of the Mage_Catalog_Model_Category class look at the file: app/code/core/Mage/Catalog/Model/Category.php.

Comments and questions welcome :)

Adding Multiple Products To The Cart Simultaneously

In a previous post I looked at creating a product on the fly and adding it to the cart automatically. However, if you are using Magento without the catalog then when you transfer customers from your catalog to the cart and checkout you may need to create and add multiple products to the cart.

Creating multiple products is fairly straightforward if you use my methodology for creating a single product from the previous post. To add multiple products to the cart, create the products and add the product objects to an array, here I have named it $products.

To then add all of these products to the cart simultaneously use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require_once ('app/Mage.php');
Mage::app();
$session = Mage:getModel('core/session', array('name' => 'frontend');
 
// create products here and add the objects to the array $products
$ids = array();
 
foreach ($products as $product)
{
 
     $ids[] = $product->getID();
 
}
 
$cart = Mage::getModel('checkout/cart');
$cart->addProductsByIDs($ids);
$cart->save();
 
// change to relevant URL for your store!
header("location: http://localhost/magento/checkout/cart/");

Setting the ‘As Low As’ Price to be the Excluding Tax Value

One of my customers is primarily B2B and they wanted the ‘as low as’ price in the catalog to show the excluding tax value, rather than the including tax value as it is by default. I could not find a way of doing this in the admin panel, so I did some digging and the solution is very straightforward, so I thought I would post it here.

Edit: frontend/default/your_theme_name/template/catalog/product/price.phtml

?Download price.xml
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php if ($this->getDisplayMinimalPrice() && $_minimalPriceValue && $_minimalPriceValue < $_product->getFinalPrice()): ?>
        <a href="<?php echo $_product->getProductUrl(); ?>" class="minimal-price-link">
            <span class="label"><?php echo $this->__('As low as:') ?></span>
            <span class="price" id="product-minimal-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>"><?php echo Mage::helper('core')->currency($_minimalPriceValue,true,false) ?></span>
        </a>
    <?php endif; /* if ($this->getDisplayMinimalPrice() && $_minimalPrice && $_minimalPrice < $_finalPrice): */ ?>
    </div>
 
<?php else: /* if (!$_product->isGrouped()): */ ?>
 
    <?php if ($this->getDisplayMinimalPrice() && $_minimalPriceValue): ?>
        <div class="price-box">
            <a href="<?php echo $_product->getProductUrl(); ?>" class="minimal-price-link">
                <span class="label"><?php echo $this->__('Starting at:') ?></span>
                <span class="price" id="product-minimal-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>"><?php echo Mage::helper('core')->currency($_minimalPriceValue,true,false) ?></span>
            </a>
        </div>
    <?php endif; /* if ($this->getDisplayMinimalPrice() && $_minimalPrice): */ ?>

Hope this helps someone! :)

Using Dropdowns for Attribute and Category Selection in Magento Catalog

There have been a few developers/ users on the Magento boards asking about alternative ways of selecting attributes, such as price or colour in the attribute filters on product catalog pages, so here is a really quick solution to convert the <ol> and <li> objects to a dropdown menus using <select> and <option>.

A quick warning though, this simple solution does rely on JavaScript.

Add the following file to: /app/design/frontend/default/_your_theme_name/template/catalog/layer

?Download filter.phtml
1
2
3
4
5
6
7
<select onchange="if(!options[selectedIndex].defaultSelected)
location=options[selectedIndex].value">
<option value="">Please Select...</option>
<?php foreach ($this->getItems() as $_item): ?>
    <option value="<?php echo substr($_item->getUrl(),stripos($_item->getUrl(),"?")) ?>"><?php echo $_item->getLabel() ?> (<?php echo $_item->getCount() ?>)</option>
<?php endforeach ?>
</select>

Any other methods/ improvements welcome!

Creating Magento Products On-The-Fly!

Sometimes it’s necessary to create products on-the-fly, typically in situations where there are so many product possibilities that even a configurable product isn’t flexible enough! In such situations the need arises to create products dynamically.

Below is the PHP code to produce a single Magento product and add it to the cart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 <?php
require_once 'app/Mage.php';
Mage::app();
 
// instatiate Product
$product = Mage::getModel('catalog/product');
 
$product->setWebsiteIds(array(1));
$product->setSku('rand-sku-' . rand());
$product->setPrice(rand(100,2000));
$product->setAttributeSetId(4); 
$product->setCategoryIds(array(3));
$product->setType('Simple Product');
$product->setName('Product Name'.rand(1,200000));
$product->setDescription('The Product Description');
$product->setShortDescription('Brief Description');
$product->setStatus(1);	
$product->setTaxClassId('2');
$product->setWeight(0);				
$product->setCreatedAt(strtotime('now'));
 
/* ADDITIONAL OPTIONS 
 
   $product->setCost();
   $product->setInDepth();
   $product->setKeywords();
 
*/
 
$product->save();
 
// "Stock Item" still required regardless of whether inventory
// control is used, or stock item error given at checkout!
 
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
 
header("Location: /checkout/cart/add/product/".$product->getId()."/); 
 
?>

This will automatically create a Magento product, with attributes and a price, add it to a number of categories, save the product, add the product to the cart and redirect the user to the Magento cart.

In a future post I will explore how to create multiple products using a set of classes and add all the products to the cart simultaneously.