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

Tuesday, November 23, 2010

Magento - Display Categories (with category images) in the homepage



<div class="top-home-category">
<?php 
/******** Don't know why I commented this
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';

if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
****************************************/
?>

<?php // Iterate all categories in store
 $limit = 0;
$_helper    = $this->helper('catalog/output');
foreach ($this->getStoreCategories() as $_category): 
// If category is Active
if($_category->getIsActive()):
// Load the actual category object for this category
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
if ($_imgUrl = $cur_category->getImageUrl()){
$_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
$_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
} 
?>
<div class="home-category">
<div class="linkimage">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
<?php echo $_imgHtml; ?>
</a>
</div>
<div class="category-link-container">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
<?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
</a>
</div>
</div>

// Load a random product from this category
/*$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->addAttributeToSelect('small_image');
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
$products->load();
// This a bit of a fudge - there's only one element in the collection
$_product = null;
foreach ( $products as $_product ) {}
?>
<div class="home-category">
<div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>">
<?php
if(isset($_product)):
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<?php
endif;
?> </a></p>
</div>
<a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a>
</div>
*/?> <?php
$limit+=1;
endif;
if ($limit==8):
break;
endif;
endforeach;
?> 
</div>

Thursday, September 9, 2010

Display Categories in the Home page

  • Display Categories in the Home page
  • Display a random product's picture with the category name
  • It can only fetch the 1st level category

-----------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN CATALOG/CATEGORY/LIST.PHTML (create it if not present) */

<?php // Iterate all categories in store
    $limit = 0;
    $_helper    = $this->helper('catalog/output');
    foreach ($this->getStoreCategories() as $_category):

        // If category is Active
        if($_category->getIsActive()):


            // Load the actual category object for this category
            $cur_category = Mage::getModel('catalog/category')->load($_category->getId());

            if ($_imgUrl = $cur_category->getImageUrl()){

                $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
                $_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
            } ?>
             <div class="home-category">
                <div class="linkimage">
                    <a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
                    <?php echo $_imgHtml; ?>
                    </a>
                </div>
                <div class="category-link-container">
                <a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
                       <?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
                </a>
                </div>
            </div>
<?php
           $limit+=1;
        endif;

        if ($limit==8):
            break;
        endif;

    endforeach;
?>

----------------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN THE CMS/PAGES/HOME*/
/* IF YOU WANT TO HARDCODE IT PLACE THIS IN LAYOUT/PAGE.XML -> "Custom page layout handles" AREA */

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


Reference

Thursday, August 26, 2010

magento bits and pieces

//this snippet will create a black bg for a product image
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->backgroundColor(array(0,0,0))->resize(135,135); ?>

//this snippet will add to cart a product in the homepage
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>')"><span ><span  style="padding:0px; font-size:12px;"><?php echo $this->__('Bestel') ?></span></span></button>

//this will get the correct translation for you custom attribute, should be enabled for product listing
$category_label = $_product->getResource()->getAttribute('category_name')->getStoreLabel($isyan);

$isyan = Mage::getModel('catalog/product')->load($_product->getId());

how to get label and value of magento attribute separately
$kwaliteit_label = $_product->getResource()->getAttribute('kwaliteit_omschrijving')->getFrontend()->getLabel($_product);
$kwaliteit_value = $_product->getResource()->getAttribute('kwaliteit_omschrijving')->getFrontend()->getValue($_product);

then echo them
echo $kwaliteit_label;
echo $kwaliteit_value;


code snippet that will ge the products of every category
_getCategoryInstance()->getProductCount() ?>

snippet that will get the homepage url of your magento website
Mage::getBaseUrl()

this snippet will load a specific directory
load(3) ?>

then you can append the methods below to get something more specific
load(3)->getUrl() ?>

methods of Magento Category

  • getName()
  • getUrl()
  • getImageUrl()
  • getProductCount()
            ////CUT THE PRODUCT NAME BECAUSE TOO LONG FOR THE TEMPLATE
          $productname = $this->htmlEscape($_product->getName());
          $productname = substr($productname , 0, 15) . '...';
  ?>

Singles

//get current controller name
$this->getRequest()->getControllerName(); //returns string

//get Action Name
$this->getRequest()->getActionName(); //returns string

//get Module Name
$this->getRequest()->getModuleName(); //returns string

//get Number of items in user cart
$this->helper('checkout/cart')->getSummaryCount(); //returns number of products

//check if user is logged in
if ($this->helper('customer')->isLoggedIn()) //returns true/false

Combinations

//check if user is on home page
if ($this->getIsHomePage()) { } //returns true/false
//check for home page only works when in header.phtml
//use method below if outside of this file or
//create new instance of:
//Mage_Page_Block_Html_Header() like so:
//$newHeaderObject = new Mage_Page_Block_Html_Header();//
if($newHeaderObject ->getIsHomePage()){} // returns true/false

//alternative method
if
(($this->getRequest()->getModuleName() == 'cms') &&
($this->getRequest()->getActionName() == 'index')) { } //returns
true/false
//use this to check if home page in other template (.phtml) files
//works by checking the module name and the action request.


//this is an alternative if getLayout is not working
<?php
$L = Mage::getSingleton('core/layout');
echo $L->createBlock('cms/block')->setBlockId('STATIC_BLOCK_IDENTIFIER')->toHtml();
?> 


Load the product attributes
$isyan=Mage::getModel('catalog/product')->load($_product->getId());

//Getting an attribute and displaying it in product view
Then use "get" + attribute code to display the value
$brandname = $isyan->getMerknaam_artikelen();