skip to Main Content

I need to get productID(s) from an order and display this way:
[1234, 7534, 4587]

I am able to get the product IDs this way:

$incrementId = "12345";
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
$items = $order->getAllItems();
$itemcount= count($items);
$meuproduto = array();
$i=0;
foreach($items as $itemId => $item) {
  $meuproduto[$i]['id'] = $item->getProductId();
    echo implode(", ", $meuproduto[$i]);
}

For example this order had products 2709 and 7048, so I would like to display:
[2709, 7048]

But with the code I have it’s showing:
27097048

I have tried str_replace("", ", ", $meuproduto[$i]);, but I get same result. I tried different ways, but always with same result.

print_r($meuproduto[$i]); 

results:

Array ( [id] => 2709 ) Array ( [id] => 7048 )

2

Answers


  1. Your echoing out each product rather than the list of products, so put the echo outside of the loop.

    You are also never changing $i, so it will always write to the first element of the array, you can just use [] to add an item to the end of an array…

    $meuproduto = array();
    foreach($items as $itemId => $item) {
      $meuproduto[] = $item->getProductId();
    }    
    echo implode(", ", $meuproduto);
    
    Login or Signup to reply.
  2. You Can Use Folowing code

    $meuproduto = array();
    
    foreach($items as $itemId => $item) {
          $id = $item->getProductId();
          array_push($meuproduto,$id);
    
    }
    print_r($meuproduto);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search