3

I am learning magento and I have some doubts about how it works, could someone help me with it or can suggest some tutorial from where I can learn more about magento. In

/app/design/frontend/../../template/catalog/product/widget/new/content/new_grid.phtml

file there is a piece of code

$this->getProductCollection()

I know this function can be found in

/app/code/core/Mage/Catalog/Block/Product/Widget/New.php

But there is no such function, I also looked in the parent class and there also I could not find this function.

I found another function in

/app/code/core/Mage/Catalog/Block/Product/Widget/New.php

Below is the function

protected function _getProductCollection()
{
    switch ($this->getDisplayType()) {
        case self::DISPLAY_TYPE_NEW_PRODUCTS:
            $collection = parent::_getProductCollection();
            break;
        default:
            $collection = $this->_getRecentlyAddedProductsCollection();
            break;
    }
    return $collection;
}

and I know this function is called but my question is why and how? These two have different names then how they can be same. Do it means that "_" is only used if a function is protected and it does not have to do anything with the function name.

And also when I echoed

$this->getDisplayType()

then I got 'new_products' but if you see in this function then magento is using self::DISPLAY_TYPE_NEW_PRODUCTS in case then how these two cases or strings are similar.

Also please explain this line

parent::_getProductCollection()

do it means call _getProductCollection() from parent class and if yes then where this is specified in magento.

1 Answer 1

2

1) Regarding: $this->getProductCollection() method:

Check file:

/app/code/core/Mage/Catalog/Block/Product/Widget/New.php

This is having _beforeToHtml() method at line no.127(Line number can be different for different version of Magento).

/**
 * Prepare collection with new products
 *
 * @return Mage_Core_Block_Abstract
 */
protected function _beforeToHtml()
{
    $this->setProductCollection($this->_getProductCollection());
    return parent::_beforeToHtml();
}

Above method sets product collection before html render.

Note: getProductCollection() and _getProductCollection() are not same. Both are completely different method.

2) $this->getDisplayType()

Any constant variable can be defined using const statement.

const DISPLAY_TYPE_NEW_PRODUCTS = 'new_products';

and that variable can be used as:

self::DISPLAY_TYPE_NEW_PRODUCTS;

So, self::DISPLAY_TYPE_NEW_PRODUCTS will return new_products.

Your Answer

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

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