I want the items that are shipped in the "sales_order_shipment_save_after" event(Not all the ordered items,only the currently shipped items). How can I do it. Here is my code. This code gives shipment id, but the shipment items are not returned.
class ProcessShipment implements ObserverInterface
{
/**
* @var eventManager
*/
protected $_eventManager;
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
protected $_shipmentItemCollectionFactory;
public function __construct(
\Magento\Framework\Event\Manager $eventManager,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->_eventManager = $eventManager;
$this->_objectManager = $objectManager;
$this->_storeManager = $storeManager;
}
public function execute(Observer $observer)
{
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$items = $this->_shipmentItemCollectionFactory->create()->setShipmentFilter($shipment->getId());
foreach($items as $item){
$id = $item->getProductId();
}
}
getAllItems
method returning an array of items. Could that be what you want?