0

I don't understand this error because for one it's in my spanish version and it's exactly the same in the english version. Another thing is that the error (below) says it was previously declared at the same place as the error.

PHP Fatal error:  Cannot redeclare isProductSpecial() (previously declared in /home/xxxx/public_html/app/design/frontend/spanish/default/template/catalog/product/view.phtml:203) in /home/xxxx/public_html/app/design/frontend/spanish/default/template/catalog/product/view.phtml on line 203

Here is the code:

   <div id="sticker-box">
        <?php   
        function isProductNew($product) {
        $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        if($todayDate >= $product->getData('news_from_date') && $todayDate <= $product->getData('news_to_date'))
        {
        return TRUE;
        }
        else
        {
        return FALSE;
        }}
        echo (isProductNew($_product)) ? '<div id="sticker-container"><div id="main-sticker-new"></div></div>' : '';
        ?>
        <?php 
        function isProductSpecial($product) {
        $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        if($todayDate >= $product->getData('special_from_date') && $todayDate <= $product->getData('special_to_date'))
        {
        return TRUE;
        }
        else
        {
        return FALSE;
        }}
        echo (isProductSpecial($_product)) ? '<div id="sticker-container"><div id="main-sticker-sale"></div></div>' : '';
        ?>
        </div>

Any idea how to correct this? Google Webmaster says this is causing many pages to have Index coverage issues.

1 Answer 1

1

I do not wish to dissuade you from learning from this question, so please take this constructively.

The way that the magento framework is built, $this, is actually the block class catalog/product_view, which if not rewritten/over-ridden, would be ./app/code/core/Mage/Catalog/Block/Product/View.php. Template files (.phtml) are processed within the presentation layer (blocks), which are defined mostly within the layout files (theme layout/*.xml). What you are doing is actually attempting to define a function within a class that already contains a function with that name. Meaning, you have one class with two functions of the exact same name.

Don't take this negatively, but what you are doing is completely incorrect in terms of development standards using Magento's framework. What you would need to do is rewrite the block (look up how to rewrite a magento block class), or extend it within a new block class that has that class as the parent. Then you can change the block to your custom extended class that could now modify the function by an override.

If you wish to experiment, you may copy the block class file View.php to the local codepool, so in your case: ./app/code/local/Mage/Catalog/Block/Product/View.php. There you may add in a new function with a non-conflicting name, or modify the currently defined function that you have attempted to re-define. The local codepool overrides all other codepools (core, and community), so that file is loaded instead of the original in the core codepool. So now, you may call that function in the template view using $this->. For example... in block class: public newTestFunction(...) { }

in template: $this->newTestFunction(...); If you are planning to output something, then don't forget the echo, for example: <div ..> <?php echo $this->newTestFunction(...); ?> </div>

Given the assumed experience level extracted from the question, you would be best off experimenting until you can grasp the concepts behind Magento's framework for theming. I am quite sure that you are not understanding most of what I have written, but wrote it so that you can search for help with what you do not understand, such as 'block class', 'presentation layer', 'theme layouts', etc.


HTML advice

Look up html usage of the attribute ID, https://dom.spec.whatwg.org/#concept-id, and see if you are using the attribute to suggested specifications.


UPDATE

I'm fairly confident that the issue is simply that the template file is being used multiple times, perhaps due to a configurable product containing multiple simple products.

This means that you would need to define the function elsewhere and use it appropriately on the product object being used during each iteration. A relatively new magento developer may use helper functions for things like this, although, I would recommend attempting to place the function in the appropriate block.

Upon further thought, it may not even be needed that you write that function. Consider reviewing the magento/app/code/core/Mage/Catalog/Block/Product/New.php block and using that for rendering new products. There is also a widget that allows you to filter and render the products that are considered new.

5
  • Your answer doesn't make me negative at all. But I don't see any isProductSpecial functions in Mage/Catalog/Block/Product/View.php. After rereading my question I forgot to say that this code works well in the english version but not in the spanish version and they are exactly alike. Yeah your reply is a little over my head : ) Thanks
    – Freejoy
    Apr 15, 2018 at 1:41
  • Since you have one theme working, and one theme not working, then it is possible that there is something different defined in the layouts. I'd consider checking the layout xml files in each theme for the product view handles. Also look for a local.xml, which would override layouts. The reason you may not be seeing the function defined could be due a parent class having defined it.
    – df2k2
    Apr 15, 2018 at 1:50
  • Perhaps start with a grep or global search for all of the function definitions in the app/code/* isProductNew()
    – df2k2
    Apr 15, 2018 at 1:53
  • Nevermind the advice above about the searching.. I have discovered that the function is not at all defined within magento's core. The issue is simply that a block is calling the view template's file multiple times.. perhaps a configurable product or something of that sort.
    – df2k2
    Apr 15, 2018 at 2:02
  • Okay, I'll check and see what I can find.
    – Freejoy
    Apr 15, 2018 at 2:26

Your Answer

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

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