Documentation

Shipment contents

Attention! It's categorically prohibited to use the method \Bitrix\Sale\ShipmentItemCollection::save() for saving shipments. When changing shipment contents, associated entities may be updated but will not be saved. Saving action must be performed via the order method: \Bitrix\Sale\Order::save(). Additionally, calling \Bitrix\Sale\ShipmentItemCollection::save() will generate error E_WARNING.

  • Receiving shipments:
  • Getting a specific shipment
  • Adding a product into shipment
  • Updating product in shipment
  • Deleting a product from shipment
  • Getting product price inside shipment
  • Setting shipment warehouse for a product
  • Receiving shipments:

    • Handling a collection
      $collection = $shipment->getShipmentItemCollection();
      foreach ($collection as $shipmentItem)
      {
      	// ...
      }
      

      Bundles within shipment collection are stored in "expanded" form: they contain the bundle itself (parent) and its contents. It must be considered when handling this collection. Special methods exist to simplify such handling to streamline the process:

      $collection = $shipment->getShipmentItemCollection();
      $collection->getShippableItems(); 
      
      /*
       * Returns collection of products for a shipment. 
       * This collection has all elements, with exception of bundle parents, 
       * i. e. they are not considered when shipping
       */
      
      
      $collection = $shipment->getShipmentItemCollection();
      $collection->getSellableItems(); 
      
      /*
       * Returns collection of products for sale. 
       * This collection has all elements, except bundle contents
       */
      
      

      Find more detailed information on handling bundles in Shopping cart.


    • Handling an array

      In addition to objects, there is an option to directly query the database via the method \Bitrix\Sale\ShipmentItemCollection::getList($parameters), or \Bitrix\Sale\ShipmentItem::getList($parameters). Both these methods work similarly.

      Methods always returns the object Bitrix\Main\DB\Result to retrieve data using the method fetch().

      Parameter Description Available from version
      $parameters Array with structure fully matching ORM's getList.
      // getting contents for shipment 123
      $dbRes = \Bitrix\Sale\ShipmentItemCollection::getList([
      	'select' => ['*'],
      	'filter' => [
      		'=ORDER_DELIVERY_ID' => 123, 
      	]
      ]);
      
      while ($item = $dbRes->fetch())
      {
      	var_dump($item);
      }
      

    Getting a specific shipment

    $collection = $shipment->getShipmentItemCollection();
    
    • by ID
      $shipment = $collection->getItemById($id);
      
    • by internal index
      $shipment = $collection->getItemByIndex($index);
      
    • by shopping cart item code
      $shipment = $collection->getItemByBasketCode($basketCode);
      
    • by shopping cart item ID
      $shipment = $collection->getItemByBasketId($basketId);
      

    Adding a product into a shipment

    $collection = $shipment->getShipmentItemCollection();
    $collection->createItem($basketItem);
    

    or

    $collection = $shipment->getShipmentItemCollection();
    		
    $item = \Bitrix\Sale\ShipmentItem::create($collection, $basketItem);
    $collection->addItem($item);
    

    Updating products in shipment

    • Updating a separate field:
      $shipmentItem->setField('FIELD_NAME', $value);
      
    • Updating group of fields:
      $shipmentItem->setFields([
      	'FIELD_NAME_1' => $value,
      	...
      	'FIELD_NAME_2' => $value,
      ]);
      
      "QUANTITY", // number of products in shipment
      "RESERVED_QUANTITY", // number of reserved product inside shipment
      "XML_ID" // XML_ID
      

    Deleting a product from shipment

    $r = $shipmentItem->delete();
    if (!$r->isSuccess())
    {
        var_dump($r->getErrorMessages());
    }
    

    Getting products cost in shipment contents

    $collection = $shipment->getShipmentItemCollection();
    $price = $collection->getPrice();
    

    Setting shipment warehouse for a product

    $order = \Bitrix\Sale\Order::load(123)
        
    $shipmentCollection = $order->getShipmentCollection();    
    $shipment = $shipmentCollection->getItemById(123);
        
    $itemCollection = $shipment->getShipmentItemCollection();    
    $shipmentItem = $itemCollection->getItemById(1);
        
    $collection = $shipmentItem->getShipmentItemStoreCollection();    
    /*
     * Consider that warehouse for shipment is not indicated. 
     * Otherwise, use the method getItemById 
     */
    $itemStore = $collection->createItem($shipmentItem->getBasketItem());
        
    $itemStore->setFields([
    	'QUANTITY' => 1,
    	'STORE_ID' => 1
    ]);
    
    $r = $order->save();
    if (!$r->isSuccess())
    {
    	var_dump($r->getErrorMessages());
    }
    


    © «Bitrix24», 2001-2024
    Up