Showing posts with label category. Show all posts
Showing posts with label category. Show all posts

Monday, December 5, 2011

Add category names in product view page

Here is an other short but useful for some people thing you can do in Magento.
Here is how you can add the category tree path in the product view page.

Edit file app/design/frontend/{interface}/{theme}/template/catalog/product/view.phtml

And in the section that you want the categories to appear add this code:
<?php $categoryIds = $_product->getCategoryIds();?>
 <?php foreach ($categoryIds as $categoryId) :?>
  <?php $tmpId = $categoryId;?>
  <?php $categories = array();?>
  <?php while($tmpId != Mage::app()->getStore()->getRootCategoryId()) : ?>
   <?php $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($tmpId);?>
   <?php $categories[] = $category;?>
   <?php $tmpId = $category->getParentId();?>
  <?php endwhile;?>
  <?php for ($i = count($categories) - 1; $i>=0;$i--) :?>
   <a href="<?php echo $categories[$i]->getUrl() ?>"><?php echo $categories[$i]->getName() ?></a>
   <?php if ($i >0 ):?>
    -&gt;<!-- this is the tree separator. change to whatever you like-->
   <?php endif;?>
  <?php endfor;?>
  <br />
  <?php //break;//uncomment this if you want only one category tree to appear.?>
 <?php endforeach;?>

I hope this helps someone.
Keep in mind that this will make your page load a little slower, but I don't think it's something you should worry about.
Any comments are welcomed.

Marius.

Tuesday, June 7, 2011

Add category names to the product list

In order to add category names to the product list (grid)
you have 2 options. For both of them you have to edit app/design/frontend/{interface}/{theme}/template/catalog/product/list.phtml
1. Add the category (categories) link(s) for all the products in any list (search results included). Keep in mind that any product can be in one or more categories (or none).
At the top of the file add this:
<?php $_category = Mage::regsitry('current_category');?>
<?php if ($_category) : ?>
<a href="<?php echo $_category->getUrl();?>"><?php echo $_category->getName();?></a>
<?php endif;?>
2.
At the top of the file add this:
<?php $_categories = array();?>
This will work as some kind of cache.
Now below the $_product item (inside the foreach) add this
<?php foreach ($_product->getCategoryIds() as $categoryId) : ?>
 <?php if (isset($_categories[$categoryId])) : ?>
  <?php $_category = $_categories[$categoryId];?>
 <?php else:?>
  <?php $_category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryId);?>
  <?php $_categories[$categoryId] = $_category;?>
 <?php endif;?>
 <a href="<?php echo $_category->getUrl()?>"><?php echo $_category->getName();?></a>
 <?php //if you want only the first category link add a break here; ?>
<?php endforeach;?>
This second method covers all the cases but it's a little bit slower than the first one.

Monday, November 23, 2009

Remove category from navigation. (Linking to non active category? for HyekiM)

This is a response for http://www.magentocommerce.com/boards/viewthread/67559/
For versions >= 1.4 this is included in the core (not this, but something similar)
Problem: Linking to a non-active category. Turned into "removing a category from navigation".
Why don't you trey an other approach. Set the category as active and remove it from the navigation.
If you are using drawItem method from 'Mage_Catalog_Block_Navigation' class to list your categories you can do this. Replace
if (!$category->getIsActive()) {
            return $html;
        }
with
if (!$category->getIsActive() || $category->getId() == YOUR CATEGORY ID) {
            return $html;
        }
This is the quick and dirty way to do it.
The clean way is to add a new 'yes/no' attribute on the category object "Show in Navigation (show_in_navigation)", and you can change the code above to:
if (!$category->getIsActive() || !$category->getShowInNavigation()) {
            return $html;
        }

To add a new attribute to the category do this:
Run this sql script on your database
INSERT INTO `eav_attribute` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `is_configurable`, `apply_to`, `position`, `note`, `is_visible_in_advanced_search`, `is_used_for_price_rules`) VALUES
(NULL, 3, 'is_anchor', NULL, '', 'int', '', '', 'select', 'Show in Navigation', NULL, 'eav/entity_attribute_source_boolean', 1, 1, 0, 0, '', 0, 0, 0, 0, 0, 1, '', 1, '', 0, 1);

The second value to insert (3) is the entity_type id for the category object. You can check if this is correct in this table:eav_entity_type.
Take the entity_type_id for entity_type_code 'catalog_category'.

After you run the sql clear the cache (var/cache).
Now you should have the "Show in Navigation" attribute for each category.

Link to first product in category, dynamically (for marcfalk)

This is a response for http://www.magentocommerce.com/boards/viewthread/67625/
Problem: Create a link to the first(smallest id) in stock product in a category.
This is the code to retrieve the first product in stock with the smallest id:
(Let's assume the category id you want to do it for is $categoryId)
$_category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($_category);
$collection->getSelect()->joinLeft(
array('_inventory_table'=>$collection->getResource()->getTable('cataloginventory/stock_item')),
"_inventory_table.product_id = e.entity_id",
array('is_in_stock', 'manage_stock')
);
$collection->addExpressionAttributeToSelect('on_top',
'(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR  ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
array());
$collection->getSelect()->order('on_top DESC');
$collection->getSelect()->order('entity_id ASC');
$collection->getSelect()->limit('1');
if ($collection->count() > 0){
$_product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($collection->getFirstItem()->getId());
}
else{
$_product = Mage::getModel('catalog/product');
}

After that in order to print the product url do this:
<?php if ($_product->getId()) : ?>
<a href="<?php echo $_product->getUrl()?>"> First in stock product in category A  (or <?php echo $_product->getName(); ?>)</a>
<?php endif;?>

In case the category has no products at all the link above will not be displayed.
In case all the products in the category are out of stock the link will point to the product with the smallest id.
if you want in this case not to display the link change the if above into:
<?php if ($_product->getId() && $_product->isSalable()) : ?>