3

I listen to this event: catalog_product_load_after and I need to retrieve the products id, I know I can retrieve the products' id with:

$collection = Mage::registry('current_category')
                       ->getProductCollection()
                       ->addAttributeToSelect('id')

But it doesn't look so convenient if the category has a lot of products, since this will run each time you change the page, so I wonder if there is a way to limit the products to be retrieved only to the current page.

Side question: $collection is just an array of numbers or I still need to loop through each element and run $product->getId()?

EDIT
What I need to do is manipulate the "add to cart" button of category and product pages according to some information, as far as I know I can do this only after the html of the page has been created
The information can be retrieved witht the product id, so I wonder if there is a way to limit the number of id that will be checked.
Inside the function I have also this:

if ($block instanceof Mage_Catalog_Block_Product_View) {..}
else if ($block instanceof Mage_Catalog_Block_Category_View){..}



EDIT 2
What I have tried:

$currentPage=Mage::registry('current_category')->getCurPage();
$currentSize=Mage::registry('current_category')->getPageSize();
$collection = Mage::registry('current_category')->getProductCollection()
                                                ->addAttributeToSelect('id')
                                                ->setPageSize($currentSize)
                                                ->setCurPage($currentPage);

Unfortunately $currentPage and $currentSize are empty

EDIT 3
I have tried:

$collection =  $observer->getProduct();;
Mage::log($collection, null, 'mylogfile.log');

but the log file is empty:

2015-05-17T18:22:37+00:00 DEBUG (7): 

I have also tried:

$collection = $observer->getEvent()->getCollection()
$collection = $observer->getCollection()



EDIT 4
First I tried:

Mage::log(var_dump($observer), null, 'mylogfile.log');

but it printed the object on the frontend and it was unreadable,then i tried:

Mage::log(var_export(array_keys( $observer->getData() ), TRUE),NULL,'mylogfile.log');

that returned:

array (
  0 => 'event',
  1 => 'block',
  2 => 'transport',
)

Finally:

Mage::log(var_export($observer->debug(), TRUE), null, 'mylogfile.log');

and this is the result:

array (
  'event (Varien_Event)' => 
  array (
    'block (Mage_Catalog_Block_Category_View)' => 
    array (
      'type' => 'catalog/category_view',
      'current_category (Mage_Catalog_Model_Category)' => 
      array (
        'store_id' => '1',
        'entity_id' => '3',
        'entity_type_id' => '3',
        'attribute_set_id' => '3',
        'parent_id' => '2',
        'created_at' => '2014-08-12T08:47:07-04:00',
        'updated_at' => '2015-01-04 18:49:11',
        'path' => '1/2/3',
        'position' => '1',
        'level' => '2',
        'children_count' => '11',
        'name' => 'Dermocosmesi',
        'url_key' => 'test',
        'display_mode' => 'PRODUCTS',
        'url_path' => 'test.html',
        'is_active' => '1',
        'include_in_menu' => '1',
        'is_anchor' => '1',
        'custom_use_parent_settings' => '0',
        'custom_apply_to_products' => '0',
        'path_ids' => 
        array (
          0 => '1',
          1 => '2',
          2 => '3',
        ),
      ),
      'module_name' => 'Mage_Catalog',
    ),
    'transport (Varien_Object)' => 
    array (
      'html' => 'HTML',
    ),
    'name' => 'core_block_abstract_to_html_after',
  ),
  'block (Mage_Catalog_Block_Category_View)' => '*** RECURSION ***',
  'transport (Varien_Object)' => '*** RECURSION ***',
)

Products ids associated with this category: 1,2, but only one product per page is displayed and so only one of those two should be retrieved

closed as too broad by Marius May 28 '15 at 8:46

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You should describe the problem you're trying to solve. The event you're listening to and the action you're describing don't really make sense. The event will fire for every product that's loaded in any context but you're working with the current category in the registry. The return type of getProductCollection is Mage_Catalog_Model_Resource_Product_Collection which is traversable, one of its parent classes implements IteratorAggregate. – beeplogic May 17 '15 at 15:13
  • @beeplogic I have added some information – Razorphyn May 17 '15 at 15:21
2

In this example, we capitalize the product name after its get loaded in the front-end.

app/code/local/Pixlpitch/ExampleObserver/Model/Observer.php

class Pixlpitch_ExampleObserver_Model_Observer
{
    public function catalogProductLoadAfter(Varien_Event_Observer $observer)
    {
        $_product=$observer->getProduct();
        return $_product->setName(strtoupper($_product->getName()));
    }
}

catalog_product_load_after is the event fired when the product is loaded. So the observer needs to listen to that event and carry out it functions.

The event is fired on the front-end. So this would not affect the product load in the backend. If you need the observer to listen product_load_after event in the backend, change the “” to “”. To listen on both backend and front-end, change it to “”.

The method listening to the event is catalogProductLoadAfter(). In this method we get the product object, retrieve the name and capitalize it.

<?xml version="1.0"?>
<config>
    <modules>
        <Pixlpitch_ExampleObserver>
            <version>1.0.0</version>
        </Pixlpitch_ExampleObserver>
    </modules>
</config>

app/code/local/Pixlpitch/ExampleObserver/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Pixlpitch_ExampleObserver>
            <version>1.0.0</version>
        </Pixlpitch_ExampleObserver>
    </modules>

    <global>
        <models>
            <exampleobserver>
                <class>Pixlpitch_ExampleObserver_Model</class>
            </exampleobserver>
        </models>
    </global>

    <frontend>  <!-- global | adminhtml | frontend -->
        <events>
            <catalog_product_load_after>   <!-- event name -->
                <observers>
                    <exampleobserver>
                        <type>model</type>     <!-- model | singleton -->
                        <class>exampleobserver/observer</class>
                        <method>catalogProductLoadAfter</method>
                    </exampleobserver>
                </observers>
            </catalog_product_load_after>
        </events>
    </frontend>
</config>
  • I didn't know about catalog/layer, however I need the products id, but this return the category one – Razorphyn May 17 '15 at 15:53
  • oky. see this Magento Load Product event observer – Supravat M May 17 '15 at 16:53
  • I have tried $observer->getData('product'), but it looks like it is empty... – Razorphyn May 17 '15 at 17:22
  • use ` $observer->getProduct();` instated $observer->getData('product') – Supravat M May 17 '15 at 18:16
  • Still doesn't work, I have updated the asnwer with some tries – Razorphyn May 17 '15 at 18:23

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