0

I need get all skus in a current category page, the code works only with getId, i want get skus: getSku, but doesnt work:

$products = Mage::getBlockSingleton('catalog/product_list')->getLoadedProductCollection();
                $category = Mage::registry('current_category');


                foreach ($products as $item) {
                        if($lista[0] != ''){
                        $lista[0][] = ' '.$item->getSku();

                        }
                        else{
                            $lista[0][] = $item->getSku();
                        }
                   $data['event'] = 'category';
                    $google_tag_params = array();       
                    $google_tag_params['ecomm_prodid'] =  $lista[0];
                    $data['google_tag_params'] = $google_tag_params;
                     }

This return: [null, ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]

What's wrong?

0

2 Answers 2

1

This should give you a collection of all the products in the current category

$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addCategoryFilter($category)
    ->setOrder('price', 'ASC')
    ->load();

You should then be able to do $item->getsku() as you loop through the collection.

This similar question might also help you https://stackoverflow.com/questions/29124232/magento-get-all-products-in-the-current-category

3
  • Woah! Exactly what I was looking for. Thx paj! Sep 23, 2015 at 14:47
  • google is my friend.
    – paj
    Sep 23, 2015 at 14:54
  • If i have a data array. $data = array(); How can i push all the skus into it... Perhaps you can help me with my question. @paj magento.stackexchange.com/questions/109807/…
    – Head
    Apr 6, 2016 at 19:32
2

To make $item->getSku() give you the correct SKU, just set "Used In Product Listing" set to "Yes" for the "sku" attribute (under Catalog=>Attributes=>Manage Attributes).

Because the attribute isn't set this way, the collection does not load it, so when you call $item->getSku() you are getting an undefined result.

The code Paj suggested may work, however, it involves making extra database queries. It also ignores the user's current pagination settings, so you might report 100 products to Google Tag Manager, even if the user only sees 9.

Possibly related question: https://stackoverflow.com/questions/6271284/can-i-add-other-attributes-to-magentos-flat-product-catalog-table

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.