5

I am setting up Google trusted stores and I am stuck on one problem which is not compulsory but I would rather it was working.

I need to get the product ID of the item the customer has ordered

So far I have this:

$orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

and I guess I need to get the product ID with something like...$order->get**productID**()

Also what would happen if there is multiple products?

6

You can do the following after your code:

$items = $order->getAllVisibleItems();
$productIds = array();
foreach($items as $i) {
      $productIds[] = $i->getProductId();
}

The $productIds array will contain all the product ids from the order.

| improve this answer | |
  • Do I need the sqaure brackets when I echo $productIds ? – Mike Tim Turner May 13 '16 at 10:31
  • @MikeTimTurner Just do print_r($productIds) to get the values of the array. – Raphael at Digital Pianism May 13 '16 at 10:33
  • echo $productIds; is what I needed. Thanks for the help – Mike Tim Turner May 13 '16 at 11:16
2

To get all products from the order, in my case I allow for all incoming orders, I did the following. Allows for more flexibility.

            $order_ids = $observer->getData('order_ids');
            foreach($order_ids as $order_id) {
                echo '<P>'.$order_id;
                $order = Mage::getModel('sales/order')->load($order_id);
                $items = $order->getAllVisibleItems();
                foreach($items as $item) {
                    echo '<P>'.$item->getProductId();
                }
              }
| improve this answer | |

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.