2

I am assembling a product array drop-down for an extension I am building. I was curious if it is possible to pull a few more attributes into the following code so that I can reference them elsewhere in my code at the same time.

Currently, I am pulling all product SKUs from Magento using the following:

$products = array();
$productCollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSort('sku', 'asc');
$i = 1;
$items = $productCollection->getData();
foreach($items as $item) {
$products[$i]['value'] = $item['sku'];
$products[$i]['label'] = $item['sku'];
$i++;
}

I would like to also include the product's entity_id, created_at & updated_at fields into this array.

ADDITIONAL NOTES: Currently I just reference the SKUs using the following code in my admin HTML:

$products = array();
$productCollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSort('sku', 'asc');
$i = 1;
$items = $productCollection->getData();
foreach($items as $item) {
$products[$i]['value'] = $item['sku'];
$products[$i]['label'] = $item['sku'];
$i++;
}
2
$productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect(array('sku','created_at','updated_at'))
    ->addAttributeToSort('sku', 'asc');

$products = array();
foreach($productCollection as $item) {
    $products[] = array(
        'entity_id' => $item->getId(),
        'value' => $item->getData('sku'),
        'label' => $item->getData('sku'),
        'created_at' => $item->getData('created_at'),
        'updated_at' => $item->getData('updated_at')
    );
}
  • Is it possible to echo out certain indexes of the array from here? For example, I wanted to echo the entity_id by doing 'echo $products[0]' or perhaps to see the created_at value I would do 'echo $products[3]'. I tried several variations but not seeing any results. The code itself works as described though which is great! – sparecycle Jan 15 '15 at 18:48
  • 1
    <?php echo $products[0]['entity_id'] ?>. Keep in mind that my logic will have the $products first array key index start at zero, whereas your original logic the original array key index would have been 1. – Darren Felton Jan 15 '15 at 18:49
0

Deeper you can convert an collection to array using function toArray Code:

$productCollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSort('sku', 'asc')
        ->addAttributeToselect('sku')->addAttributeToselect('entity_id');

$items=$productCollection->toArray();

foreach($items as $item):
    echo $item['sku'];        
    echo $item['entity_id'];
endforeach;
  • There is no need for ->addAttributeToSelect('entity_id'). All resource model collections will include the entity's primary key as part of the select query automatically. – Darren Felton Jan 15 '15 at 17:07

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.