Paulo
Paulo

Reputation: 115

Magento - List a product collection without the foreach

I have a product collection loaded with 6 products. I need to list these products without the foreach, because each product have a different layout structure. How can I call each product outside the foreach?

My code is:

<?php

  $produtos = array(1115,1105,1019,1017,1013,1011);
  $_productCollection=$this->getLoadedProductCollection();
  $_productCollection = clone $this->getLoadedProductCollection();
  $_productCollection->clear()
                     ->addIdFilter($produtos)
                     ->load();
  $_helper = $this->helper('catalog/output');
?>

The variable $produtos is an array with the product IDs that I want in the collection

Then, i need to set variables for each product attribute. Im doing like this:

<?php foreach ($_productCollection as $_product): ?>
    <?php 
        if($_product->getId() == $produtos[0]){
            $productName1 = $_helper->productAttribute($_product, $_product->getName(), 'name'); 
            $productImage1 = '<img border="0" src=".'echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(143,215);'." />';
            $precoCru1 = $_product->getFinalPrice(); $productPrice1 = Mage::helper('core')->currency($precoCru1, true, false);
            $productUrl1 = $_product->getProductUrl();
        }

        elseif($_product->getId() == $produtos[1]){
            $productName2 = $_helper->productAttribute($_product, $_product->getName(), 'name'); 
            $productImage2 = '<img border="0" src=".'echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(143,215);'." />';
            $precoCru2 = $_product->getFinalPrice(); $productPrice1 = Mage::helper('core')->currency($precoCru1, true, false);
            $productUrl2 = $_product->getProductUrl();                      
        }
    ?>                  
<?php endforeach; ?>

The product name, url and price are working, but the image not. When I echo each product image, it gave me the same URL

Upvotes: 0

Views: 2039

Answers (2)

Adrian Marina
Adrian Marina

Reputation: 26

You can try to get the items and then use them as a regular array and refer to the elements by their index:

$items = $collection->getItems();
$first = $items[0];
$second = $items[1];

or if you need just the first one:

$first = $collection->getFirstItem();

Have a look at lib/Varien/Data/Collection.php

Upvotes: 0

Alana Storm
Alana Storm

Reputation: 166076

You can't avoid the foreach entirely, because that's not how programing works, but give this a try

$array = array();
$c=1;
foreach($product_collection as $product)
{
    $array['product_'.$c] = $product;
    $c++;
}

and you'll be able to reference each individual product

var_dump($array['product_1']->getData());
var_dump($array['product_2']->getData());

If you really want to confuse the next programmer, try the above code, but then do this

extract($array);
var_dump($product_1->getData());

The extract function will export each key of the array as a PHP variable.

Upvotes: 3

Related Questions