Skip to content

Instantly share code, notes, and snippets.

@jacquesbh
Last active December 14, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacquesbh/5033402 to your computer and use it in GitHub Desktop.
Save jacquesbh/5033402 to your computer and use it in GitHub Desktop.
[Blog] Bien afficher un attribut personnalisé http://jacques.sh/2013/02/afficher-un-attribut.html
<!-- ... -->
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
<?php echo $this->helper('catalog/output')->productAttribute($product, $product->getOurAttributeCode(), 'our_attribute_code'); ?>
<?php
class Jbh_ProductAttribute_Helper_Output extends Mage_Core_Helper_Abstract
{
/**
* Change the output of our attribute
* @param Mage_Catalog_Helper_Output $outputHelper The helper
* @param string $attributeHtml The HTML
* @param array $params The parameters
* @access public
* @return string
*/
public function productAttribute(Mage_Catalog_Helper_Output $outputHelper, $attributeHtml, array $params)
{
// Just for us ;)
if ($params['attribute'] == 'our_attribute_code') {
// The product
$product = $params['product'];
// Already processed?
if (null !== $html = $product->getOurAttributeCodeProcessed()) {
return $html;
}
// Create our block
$block = Mage::app()->getLayout()->createBlock('cms/block');
// Set the CMS block identifier
$block->setBlockId($product->getOurAttributeCode());
// Generate the HTML
$html = $block->toHtml();
// op! in the product
$product->setOurAttributeCodeProcessed($html);
// Return the HTML
return $html;
}
// Continue the other attributes
return $attributeHtml;
}
}
<?php
class Jbh_ProductAttribute_Model_Observer
{
/**
* Add attribute handler
* @param Varien_Event_Observer $event Our event
* @access public
* @return void
*/
public function addAttributeHandler(Varien_Event_Observer $event)
{
$event->getHelper()->addHandler('productAttribute', Mage::helper('jbh_productattribute/output'));
}
}
<?php
/* ... */
class Mage_Catalog_Helper_Output extends Mage_Core_Helper_Abstract
{
/* ... */
/**
* Adding method handler
*
* @param string $method
* @param object $handler
* @return Mage_Catalog_Helper_Output
*/
public function addHandler($method, $handler)
{
if (!is_object($handler)) {
return $this;
}
$method = strtolower($method);
if (!isset($this->_handlers[$method])) {
$this->_handlers[$method] = array();
}
$this->_handlers[$method][] = $handler;
return $this;
}
/* ... */
}
<?php
/* ... */
class Mage_Catalog_Helper_Output extends Mage_Core_Helper_Abstract
{
/* ... */
/**
* Constructor
*/
public function __construct()
{
Mage::dispatchEvent('catalog_helper_output_construct', array('helper'=>$this));
}
/* ... */
}
<?php
/* ... */
class Mage_Catalog_Helper_Output extends Mage_Core_Helper_Abstract
{
/* ... */
/**
* Process all method handlers
*
* @param string $method
* @param mixed $result
* @param array $params
* @return unknown
*/
public function process($method, $result, $params)
{
foreach ($this->getHandlers($method) as $handler) {
if (method_exists($handler, $method)) {
$result = $handler->$method($this, $result, $params);
}
}
return $result;
}
/* ... */
}
<?php
/* ... */
class Mage_Catalog_Helper_Output extends Mage_Core_Helper_Abstract
{
/* ... */
/**
* Prepare product attribute html output
*
* @param Mage_Catalog_Model_Product $product
* @param string $attributeHtml
* @param string $attributeName
* @return string
*/
public function productAttribute($product, $attributeHtml, $attributeName)
{
$attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeName);
if ($attribute && $attribute->getId() && ($attribute->getFrontendInput() != 'media_image')
&& (!$attribute->getIsHtmlAllowedOnFront() && !$attribute->getIsWysiwygEnabled())) {
if ($attribute->getFrontendInput() != 'price') {
$attributeHtml = $this->escapeHtml($attributeHtml);
}
if ($attribute->getFrontendInput() == 'textarea') {
$attributeHtml = nl2br($attributeHtml);
}
}
if ($attribute->getIsHtmlAllowedOnFront() && $attribute->getIsWysiwygEnabled()) {
if (Mage::helper('catalog')->isUrlDirectivesParsingAllowed()) {
$attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
}
}
$attributeHtml = $this->process('productAttribute', $attributeHtml, array(
'product' => $product,
'attribute' => $attributeName
));
return $attributeHtml;
}
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment