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?

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

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, privacy policy and cookie policy

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