0

I am quite newbie to the Magento. Currently I am doing a theme. In that I want to show all the products from featured category. For that I have done a file called featred.phtml inside /app/design/frontend/mytheme/default/template/catalog/product. The code for featured.phtml is like this

<?php $cat_id = 35; ?>
<?php $category = Mage::getModel('catalog/category')->load($cat_id);?>
<?php $collection = $category->getProductCollection()->addAttributeToSort('position');?>
<?php Mage::getModel('catalog/layer')->prepareProductCollection($collection);?>
<?php  $i=0; foreach ($collection as $_product):?>
<?php if($i++%7==0): ?>
 <div class="container">
  <div id="da-slider" class="da-slider">
  <?php endif ?>
  <div class="da-slide">
    <h2 class="product-name"><?php echo $this->htmlEscape($_product->getName()) ?></h2>
    <p class="price"><?php echo $formattedSpecialPrice = Mage::helper('core')->currency($_product->getFinalPrice(),true,false);?></p>
    <p><?php echo $_product->_data['short_description']; ?> </p> <br />

    <a class="da-link" href="<?php echo $_product->getProductUrl() ?>">Shop Now</a>

    <div class="da-img"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(90, 90); ?>" width="120" height="120" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></div>

    <!--.da-img-->

   </div><!--.da-slide-->
   <?php endforeach ?>
    <nav class="da-arrows">
      <span class="da-arrows-prev"></span>
      <span class="da-arrows-next"></span>
    </nav><!--.da-arrows-->
  </div><!--#da-slider-->
 </div><!--.container-->

Now Here I am getting al the featured product. After that I want to show all the featured product in my header. For that I have made a block in page.xml(app/design/frontend/mytheme/default/layout) like this

<block type="catalog/product_featured" name="product.featured" as="topSlider" translate="label">
  <label>Page Slider</label>
  <action method="setElementClass"><value>top-slider</value></action>
</block>

Now I called that block in header.phtml like this

<?php echo $this->getChildHtml('topSlider'); ?>

But after all this I can't see any of the featured product in my home page or in any other pages. So can someone kindly tell me how to solve this issue? Any help and suggestions will be really appreciable. Thanks

1 Answer 1

2

I think the main problem is that you should start to understand Magento's layout structure before you begin such a task.

I will try to give a brief tutorial; It may contain some errors and is mainly for structure.

First things first:
- You should turn on Magento Developer Mode.
- You should turn on Magento logging.

After that: You are trying to create a new block. A block in Magento is not created simply by creating a template .php-file for it; instead, you will have to write a tiny module for it. There are some good tutorials out there how to create a Magento module, so I won't go deeper into detail about the config files and stuff.

How you create a block with a module: Your module must contain a "Block"-folder and a file like this: app/code/local/[Companymame]/[Modulename]/Block/Catalog/Product/Featured.php

This Featured.php holds your business logic - another thing you are mixing up here: The featured.php in the /template/-folder should contain Template-Stuff, mainly to position the products in some fancy html, display some information (name, price, whatever), but should not contain the way to get the product; instead, there should be some line like $collection = $this->getProductCollection();

Featured.php should contain the class declaration for this block and the methods;

<?php
class [Companyname]_[Modulename]_Block_Catalog_Product_Featured {

function getProductCollection() {

// get the products and return them 
$cat_id = 35
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = $category->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);

return $collection;

}
}

You have now your block class. In your module's config.xml, add a paragraph and tell Magento that this module comes with a new block and a layout update:

<global>
    <blocks>
       <featured>
           <class>[Companyname]_[Modulename]_Block_Catalog_Product_Featured</class>
        </featured>
    </blocks>
    </global>
        <frontend>
                <layout>
                    <updates>
                        <featured>
                            <file>featured.xml</file>
                        </featured>
                    </updates>
                </layout>
        </frontend>

Magento's autoloader function checks the classname and automatically gets your block-file - no manual include of Featured.php is necessary.

The mentioned file featured.xml goes into the app/design/frontend/[package]/[theme]/layout/ folder and should contain the layout update. I don't know the correct path to the header-block atm and will leave it commented for this reason, but I hope you get the point:

<?xml version="1.0">
<!-- navigate to page/header block -->
<block type="[companyname]/[modulename]_catalog_product_featured" name="product.featured" as="topSlider" translate="label">
  <label>Page Slider</label>
  <action method="setTemplate"><template>catalog/product/featured.phtml</template></action>
</block>
<!-- close open nodes -->

Notice the differences to your xml-statement: The blockname fits the classname; I also added the template via the addTemplate-action and changed you featured.php to featured.phtml - do it the Magento way ;) (not sure if .php would work, though).

As I said above: The code I provided won't be free of errors, just typing this on a lazy Saturday evening ;) I hope you get the point, ask me questions if you need further help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.