3

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();    
        }

}
  • The shipment model has a getAllItems method returning an array of items. Could that be what you want? – Aaron Allen Jan 30 '17 at 7:10
  • But it returns all the ordered items – Jancy Abraham Jan 30 '17 at 7:48
3
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();

        foreach ($shipment->getItemsCollection() as $item) {    
            $id = $item->getOrderItem()->getProductId();    
        }
    }

}
  • $shipment->getItemsCollection() returns all the ordered items. If I do a partial shipment,this returns the all ordered items. – Jancy Abraham Jan 30 '17 at 10:12
  • Please refer \Magento\Sales\Model\Order\Shipment. If you have instance of this than getItemsCollection method will return items connected with that shipment only. – Chandresh P. Jan 30 '17 at 13:38

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.