Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Display featured product in magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 298
    Comment on it

    To show a featured product first we need to set products as featured . To do so we need to create the product attribute named as "featured".

    Create an attribute-

    1) Create a new attribute by going to Catalog > Attributes > Manage Attributes > Add New Attribute.


    Attribute Properties

    Attribute Identifier: featured
    Scope: Store View
    Catalog Input Type for Store Owner: Yes/No
    Unique Value (not shared with other products): No
    Values Required: No
    Input Validation for Store Owner: None
    Apply To: All Product Types

    Front End Properties

    Use in quick search: No
    Use in advanced search: Yes
    Comparable on Front-end: No
    Use In Layered Navigation (Can be used only with catalog input type 'Dropdown'): No
    Visible on Catalog Pages on Front-end: Yes

    Manage Label/Options

    Default: Featured Product
    English: Featured Product

    2) Now we have to add newly created attribute to the attribute set. Go to Catalog > Attributes > Manage Attributes Sets to add the attribute to the default feature set.

    3) Create a new file, and directories: app/code/local/MyPackage/Catalog/Block/Product/Featured.php

    class MyPackage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract {
          public function getSubCategory($category_id) {
            $idList = "";
            $categories = '';
            $layer = Mage::getSingleton('catalog/layer');
            $category = Mage::getModel('catalog/category')->load($category_id);
            $categories = $category->getChildren();
            $idList = $categories;
            $list = explode(",", $categories);
            foreach ($list as $ccid) {
                $cat = Mage::getModel('catalog/category')->load($ccid);
                if ($cat->getChildren())
                    $idList .= "," . $cat->getChildren();
            }
            return $idList;
        }
    
        public function getFeaturedProduct() {
            $storeId = Mage::app()->getStore()->getId();
            $ccontroller = Mage::app()->getFrontController()->getRequest()->getControllerName();
            $categoryId = $this->getRequest()->getParam('id', false);
            if ($ccontroller == "product") {
                $productId = $this->getRequest()->getParam('id', false);
                $cproduct = Mage::getModel('catalog/product')->load($productId);
                $categoryArr = $cproduct->getCategoryIds();
                $categoryId = $categoryArr[0];
    
                if ($cproduct->getTypeId() == 'bundle') {
                    $bundleFlag = 1;
                    $bundleCategoryId = $categoryArr[0];
                }
            }
            $resource = Mage::getSingleton('core/resource');
            $product    = Mage::getModel('catalog/product');
            $currentCategory = Mage::getModel('catalog/category')->load($categoryId);
            $isTopCat = 0;
            $categoryLevel = $currentCategory->getLevel();
            if ($categoryLevel == 2)
                $isTopCat = 1;
            $csubcats = $currentCategory->getChildren();
            if (!empty($csubcats) || $ccontroller == "product") {
                $subcats = 0;
                if ($categoryLevel >= 2) {
                    $pathArr = explode("/", $currentCategory->getPath());
                    $subcats = $this->getSubCategory($pathArr[2]);
                }
                $categoryId = $this->getRequest()->getParam('id', false);
                $resource = Mage::getSingleton('core/resource');
                $read = $resource->getConnection('catalog_read');
                $categoryProductTable = $resource->getTableName('catalog/category_product');
                $productEntityIntTable = (string) Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
                $eavAttributeTable = $resource->getTableName('eav/attribute');
                if (empty($subcats))
                    $subcats = '0';
    
                if ($bundleFlag == 1) {
                    $subcats = $bundleCategoryId;
                }
                $select = $read->select()
                        ->from(array('cp' => $categoryProductTable))
                        ->join(array('pei' => $productEntityIntTable), 'pei.entity_id=cp.product_id', array())
                        ->joinNatural(array('ea' => $eavAttributeTable))
                        ->where('cp.category_id IN (' . $subcats . ')')
                        ->where('pei.value=1')
                        ->where('ea.attribute_code="featured"');
                $featuredProductData = $read->fetchAll($select);
                $i = 0;
                $product = array();
                $productid = array();
                foreach ($featuredProductData as $row) {
                    $productid[$i] = $row['product_id'];
                    $i++;
                }
                $productid = array_unique($productid);
                $i = 0;
                $prodObj = '';
                foreach ($productid as $id) {
                    if ($id != '') {
                        $prodObj = Mage::getModel('catalog/product')->load($id);
                        if ($prodObj->getVisibility() == 4 && $prodObj->isSaleable() == 1) {
                            $product[$i] = $prodObj;
                            $i++;
                        }
                    }
                }
            } else {
                $product = '';
            }
            return $product;
        }
    
    }
    

    4) Lets create the template file for featured product listing.

    Go to the directory and create "/app/design/frontend/mytheme/template/catalog/product/featured.phtml" and put below code in a file as per your design of page.

       echo $this->__("Featured products");
    
    $product = $this->getFeaturedProduct();
    if ($product) {
        foreach ($product as $_product) {
            echo "Product Name" . $_product->getName() ."
    "; echo "Product Price". $this->getPriceHtml($_product) .  "
    "; echo "Product short Description" . $_product->getShortDescription() .  "
    "; echo "Product Image" .  $this->helper('catalog/image')->init($_product, 'small_image')->resize(200);   . "
    "; echo "Product URL" .  $_product->getProductUrl() . "
    "; } }

    5) Add entry to local.xml file located at app/etc/. Add following set of lines inside the global tags.

    < blocks >
        < catalog >  
            < rewrite >  
                < product_featured >MyPackage_Catalog_Block_Product_Featured< /product_featured >   
            < /rewrite >  
        < /catalog >  
    < /blocks >
    
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: