Magento 2 - Getting customerId and product id from order object in observer?

Manish picture Manish · Mar 31, 2016 · Viewed 7.8k times · Source

In an Observer I want to get the product id and customer id from order object...how can I get in Magento 2.

I am not able to get it from order object

Answer

Manish picture Manish · Mar 31, 2016

Here got answer

    $order = $observer->getEvent()->getOrder(); 
    $items =$order->getAllVisibleItems();
    //product ids
    $productIds = array();
    foreach($items as $item) {
        $productIds[]= $item->getProductId();
    }

    //custoemr id
    $customerId = $order->getCustomerId();