Heyo, been working on a magento theme for a little while now (... and my understanding of the system is slowly but steadily growing). I need to start working on some custom modules soon, and so I'm starting with this little project to get my bearings:I found a little bit of code to manage a specific task that I have been copying and pasting into template files: \[code\]<?php $ids = $_product->getCategoryIds(); $cat = Mage::getModel('catalog/category')->load($ids[0]); $isFF = false; foreach ($ids as $key=>$val) { $cat = Mage::getModel('catalog/category')->load($val); $name = $cat->getName(); if ( $name === 'Fast Fulfillment' ) { $isFF = true; } } if ($isFF) { echo '<span class="ff-logo"></span>'; } ?>\[/code\]Very simple. I'm just checking to see if a product is in a specific category, and producing an element if it is. (I know there are about 5 solid ways of doing this... this is the one I went with). I need to test this every time a product block is displayed, and have, up till now, been replicating this code to make it work. It does work, but is backasswards (I shouldn't be putting logic into the view layer). Ok - so, lets make a simple module to share the functionality:local/WACI/ProductFlag/etc/config.xml\[code\]<config> <modules> <WACI_ProductFlag> <version>0.1.0</version> </WACI_ProductFlag> </modules> <global> <blocks> <WACI_ProductFlag> <class>WACI_ProductFlag_Block_View</class> </WACI_ProductFlag> </blocks> </global></config>\[/code\]etc/modules/WACI_All.xml\[code\]<config> <modules> <WACI_CustomPageLayouts> <codePool>local</codePool> <active>true</active> </WACI_CustomPageLayouts> </modules> <modules> <WACI_ProductFlag> <codePool>local</codePool> <active>true</active> </WACI_ProductFlag> </modules></config>\[/code\]Now, for the Class ... I'm not really sure if a \[code\]Block\[/code\] or a \[code\]Helper\[/code\] is appropriate. I went with \[code\]Block\[/code\], but - idk... I'm probably wrong (tutorials for this stuff vary wildly). local/WACI/ProductFlag/Block/View.php\[code\]<?php /** * WACI * * @codepool Local * @category View * @package WACI * @module ProductFlag */ class WACI_ProductFlag_Block_View extends Mage_Core_Block_Template { private $_focus; private $_isFF = false; public function getIsFF( $product ){ $this->_focus = "FF"; $isFF = false; $ids = $product->getCategoryIds(); $cat = Mage::getModel('catalog/category')->load($ids[0]); foreach ($ids as $key=>$val) { $cat = Mage::getModel('catalog/category')->load($val); $name = $cat->getName(); if ( $name === 'Fast Fulfillment' ) { $isFF = true; } } } protected function _toHtml(){ $html = parent::_toHtml(); if ($this->_focus === "FF") { if ($this->_isFF){ $html .= '<span class="ff-logo"></span>'; } } return $html; } }?>\[/code\]Hopefully its clear that I only want to output a string based on the input of any given product. Should I be overriding the \[code\]_toHtml()\[/code\] to deliver the string? Again. Probably not...in my local.xml I reference the block:\[code\]<catalog_product_view><reference name="content"> <reference name="product.info"><block type="WACI/ProductFlag" name="product.flag" as="productFlag" output="toHtml" />...\[/code\]... I'm not clear if this instantiates this class? I don't think so. I'm not really sure how to address it in my product/view/media.phtml anyway, given that I need to call a method with a parameter. I don't think I need a template file, given that I'm just outputting a string, but I don't think I've seen block modules without an associated template. Maybe a helper class is appropriate then?Whatever the case, it ain't workin.
I either get a fatal error saying my media class has no \[code\]getIsFF()\[/code\] method (not surprising), or just nothing shows up. My config files are correct, but that's about it, I think.Jebus. I'm all turned around. Can someone with Mage skillz clarify this simple issue and point me in the right direction? Cheers!
I either get a fatal error saying my media class has no \[code\]getIsFF()\[/code\] method (not surprising), or just nothing shows up. My config files are correct, but that's about it, I think.Jebus. I'm all turned around. Can someone with Mage skillz clarify this simple issue and point me in the right direction? Cheers!