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

}
3
  • The shipment model has a getAllItems method returning an array of items. Could that be what you want? Jan 30, 2017 at 7:10
  • But it returns all the ordered items Jan 30, 2017 at 7:48
  • @JancyAbraham how did you acheive these plz let me know
    – User0434
    Nov 8, 2021 at 9:40

1 Answer 1

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

}
3
  • $shipment->getItemsCollection() returns all the ordered items. If I do a partial shipment,this returns the all ordered items. Jan 30, 2017 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. Jan 30, 2017 at 13:38
  • @ChandreshP., how to get shipment Id here? i used this $shipment->getId(); but not working
    – Manjunath
    Nov 4, 2020 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.