2

I have a custom function in magento, how I can do in the frontend to see the public functions values?

Function:

public function getOptionsWithValues()
{
    $attributes = $item->getOptionByCode('attributes')->getValue();

    if (!$attributes)
    {
        return null;
    }

    $attributes = unserialize($attributes);
    $options = array();

    foreach ($attributes as $attr_id => $attr_value)
    {
        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attr_id);

        $attr_options = $attribute->getSource()->getAllOptions(false);   

        foreach ($attr_options as $option)
        {
            if ($option['value'] == $attr_value)
            {
                $options[$attribute->getFrontendLabel()] = $option['label'];
            }
        }
    }

    return $options;
}

Thank you

Robert
  • 812
  • 3
  • 18
  • 47
  • You can have this code in the Helper class of your module. You can then call it like this: `Mage::helper("helpername")->getOptionsWithValues();` – paskl Apr 26 '16 at 11:28
  • I add this in core wishlist, /app/code/core/Mage/Wishlist/Model/Item/Option.php and I want to display the attributes values in frontend – Robert Apr 26 '16 at 11:30
  • 1
    Dont hack code into core files. Create a module, insert the code above into the `Data.php` (which is your helper class for your module) and call it in frontend anywhere you want like I showed you in my first comment. – paskl Apr 26 '16 at 11:31
  • don't worry is not a problem about this, all I want is to tell me if you know how I can display $option['label'] in the frontend – Robert Apr 26 '16 at 11:33
  • `echo $options["attribute_name"]` ? – paskl Apr 26 '16 at 11:35
  • :) but that is array value, and this function is get all attributes that is related with the actual product, so we can;t use there attribute_name – Robert Apr 26 '16 at 11:37
  • How would I know what `$item` is? It isnt even in the parameters. – paskl Apr 26 '16 at 11:39
  • take a look here: * @var Mage_Wishlist_Model_Item $item */ $item = $this->getItem(); $product = $item->getProduct(); – Robert Apr 26 '16 at 11:40
  • Sorry I really dont understand what you're trying to do. Can you please add clarification about this in the question (not comment section)? – paskl Apr 26 '16 at 11:41
  • please take a look at my second comment, I will paste it again here: " I add this in core wishlist, /app/code/core/Mage/Wishlist/Model/Item/Option.php and I want to display the attributes values in frontend", is clear now? – Robert Apr 26 '16 at 11:43

2 Answers2

1

As you said you added this code in /app/code/core/Mage/Wishlist/Model/Item/Option.php. So you can instantiate this class using the factory methods like

$itemOption = Mage::getModel('wishlist/item_option');

If you try this code in a separate file and echo get_class($itemOption), you will see the class name. Now you can access the function directly by the object like $itemOption -> getOptionsWithValues().

But you should never make changes in the core files directly rather you could copy the same folder structure in the local folder or rewrite the model class which you want to override.

Shrikant
  • 384
  • 2
  • 10
0

In Magento if you want to display certain values by calling a function. you need to define that function in block

  • .phtml file : from this template file you can call any block function.

If this function is defined in a block. call it from phtml file using this code.

$blockObj = $this->getLayout()->getBlock('block_name'); 

Call the block function

echo $blockObj->getOptionsWithValues();