Yurii Makoviychuk Yurii Makoviychuk - 2 months ago 14
PHP Question

How get new installed product attribute in magento (using helper) on category pages

I've installed new product attribute using my module script - mysql4-install-1.0.0.php:

<?php
$installer = $this;
$installer->startSetup();
$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
//Setup Product Attribute
$setup->addAttribute('catalog_product', 'product_display_price', array(
'group' => 'Prices',
'label' => 'Webdevelop Extensions - Display Price',
'type' => 'int',
'input' => 'select',
'backend' => '',
'frontend' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
));
$installer->endSetup();


This attribute ('product_display_price') I can take on product pages(using var_dump(getProductAttributeValueDisplayPrice()) on my rewrite block), but when I take it on category pages I get null.
I use helper file (Data.php):

public function getProductAttributeValueDisplayPrice()
{
$currentProduct = Mage::registry('current_product');
if ($currentProduct) {
$product_id = $currentProduct->getId();
$product = Mage::getModel('catalog/product')
->load($product_id);
$attribute = $product->getData('product_display_price');
return $attribute;
}else null;
}

Answer

In my helper file - Data.php I added two function which set and get product

protected $_currentProduct = '';

public function setCurrentProduct($label)
{
    $this->_currentProduct = $label;
    return $this;
}

public function getCurrentProduct()
{
    return $this->_currentProduct;
}

In my block file I set product

$helper->setCurrentProduct($this->getProduct());

And I maked chenge in my helper function getProductAttributeValueDisplayPrice() - $currentProduct = $this->getCurrentProduct();

public function getProductAttributeValueDisplayPrice()
{
    $currentProduct = $this->getCurrentProduct();
    if ($currentProduct) {
        $product_id = $currentProduct->getId();
        $product = Mage::getModel('catalog/product')
            ->load($product_id);
        $attribute = $product->getData('product_display_price');
        return $attribute;
    }else null;
}

Everything works fine.