Documentation

Orders

  • Creating order
  • Creating order without shipments and payments
  • Getting order by its ID
  • Getting order information
  • Editing order
  • Order payment
  • Allowing order shipment
  • Adding order comment
  • Updating order status
  • Cancelling order
  • Deleting order
  • Getting public link order
  • Selecting orders by property value
  • Creating order

     
    $products = [
    	[
    		'PRODUCT_ID' => 135, 
    		'PRODUCT_PROVIDER_CLASS' => '\Bitrix\Catalog\Product\CatalogProvider'
    		'NAME' => 'Товар 1', 
    		'PRICE' => 500, 
    		'CURRENCY' => 'USD', 
    		'QUANTITY' => 5, 
    	]
    ];
    
    $basket = Bitrix\Sale\Basket::create('s1');
    
    foreach ($products as $product)
    {
    	$item = $basket->createItem("catalog", $product["PRODUCT_ID"]);
    	unset($product["PRODUCT_ID"]); 
    	$item->setFields($product);
    }
    
    $siteId = 's1'; // site ID
    $userId = 1; // user ID
    $order = \Bitrix\Sale\Order::create($siteId, $userId);
    
    $order->setPersonTypeId(1); // 1 - payer type ID
    
    $order->setBasket($basket);
    
    $shipmentCollection = $order->getShipmentCollection();
    $shipment = $shipmentCollection->createItem(
    	Bitrix\Sale\Delivery\Services\Manager::getObjectById(1) // 1 - delivery service ID
    );
    
    $shipmentItemCollection = $shipment->getShipmentItemCollection();
    
    foreach ($basket as $basketItem)
    {
    	$item = $shipmentItemCollection->createItem($basketItem);
    	$item->setQuantity($basketItem->getQuantity());
    }
    
    $paymentCollection = $order->getPaymentCollection();
    $payment = $paymentCollection->createItem(
    	Bitrix\Sale\PaySystem\Manager::getObjectById(1) // 1 - payment system ID
    );
    
    $payment->setField("SUM", $order->getPrice());
    $payment->setField("CURRENCY", $order->getCurrency());
    
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
    

    Creating order without shipments and payments

    $basket = \Bitrix\Sale\Basket::create('s1');
    
    $item = $basket->createItem('catalog', 123);
    $item->setField('QUANTITY', 1);
    $item->setField('CURRENCY', 'USD');
    $item->setField('PRODUCT_PROVIDER_CLASS', '\Bitrix\Catalog\Product\CatalogProvider');
    
    $item = $basket->createItem('catalog', 125);
    $item->setField('QUANTITY', 1);
    $item->setField('CURRENCY', 'USD');
    $item->setField('PRODUCT_PROVIDER_CLASS', '\Bitrix\Catalog\Product\CatalogProvider');
    
    $basket->refresh();
    
    $order = \Bitrix\Sale\Order::create('s1', 1, 'USD');
    $order->setPersonTypeId(1);
    $order->setBasket($basket);
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
    

    Getting order by its ID

    Order is an object to be retrieved by several ways:

    • по ID:
      $order = \Bitrix\Sale\Order::load($id);
    • по номеру заказа (ACCOUNT_NUMBER):
      $order = \Bitrix\Sale\Order::loadByAccountNumber($accountNumber);

    Getting order information

    Use one of two methods to get information.

    • \Bitrix\Sale\Order::loadByFilter($parameters);

      Method always returns array of objects, or null.

      Parameter Description Available from version
      $parameters Array, structure:
      • filter - description of filter for WHERE и HAVING
      • order - sorting parameters
      • limit - number of entries
      • offset - offset for limit
      • runtime - dynamically defined fields

      \Bitrix\Sale\Order::getList($parameters);

      Method always returns object DB\Result, from which to get data using the method fetch();

      Parameter Description Available from version
      $parameters Array, with structure fully matching the ORM's getList.
      Method always returns object DB\Result, used to get data using the method fetch().

      Examples:

      Input dates for all orders for current user for current month, sorted by order date.

      	 
      $parameters = [
      	'filter' => [
      		"USER_ID" => $USER->GetID(),
      		">=DATE_INSERT" => date($DB->DateFormatToPHP(CSite::GetDateFormat("SHORT")), mktime(0, 0, 0, date("n"), 1, date("Y")))
      	],
      	'order' => ["DATE_INSERT" => "ASC"]
      ];
      
      $dbRes = \Bitrix\Sale\Order::getList($parameters);
      while ($order = $dbRes->fetch())
      {
      	var_dump($order);
      }
      

      Get orders with specific product

       
      $dbRes = \Bitrix\Sale\Order::getList([
      	'filter' => [
      		'BASKET.PRODUCT_ID' => 174474
      	],
      	'order' => ['ID' => 'DESC']
      ]);
      	 
      while ($order = $dbRes->fetch())
      {
      	var_dump($order);
      }
      	
      

      Filtering by several status variants

       
      $dbRes = \Bitrix\Sale\Order::getList([
      	'filter' => [
      		"USER_ID" => $USER->GetID(), 
      		"@STATUS_ID" => ["N", "P"]
      	],
      	'order' => ['ID' => 'DESC']
      ]);
      	 
      while ($order = $dbRes->fetch())
      {
      	var_dump($order);
      }
      	 
      

      Getting list of orders with a missing property

      
      $dbRes = \Bitrix\Sale\Order::getList([
      	'select' => ['ID'], 
      	'filter' => [
      		'PROPERTY_VAL.ID' => null
      	],
      	'runtime' => [
      		new \Bitrix\Main\Entity\ReferenceField(
      			'PROPERTY',
      			'\Bitrix\sale\Internals\OrderPropsTable',
      			array("=ref.PERSON_TYPE_ID" => "this.PERSON_TYPE_ID",),
      			array("join_type"=>"inner")
      		),
      		new \Bitrix\Main\Entity\ReferenceField(
      			'PROPERTY_VAL',
      			'\Bitrix\sale\Internals\OrderPropsValueTable',
      			array("=this.PROPERTY.ID" => "ref.ORDER_PROPS_ID", "=this.ID" => "ref.ORDER_ID",),
      			array("join_type"=>"left")
      		),
      	],
      	'group' => ['ID'],
      	'order' => ['ID' => 'DESC']
      ]);
      	
      while ($order = $dbRes->fetch())
      {
      	var_dump($order);
      }
      

    Editing an order

    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
     
    $order->setField('USER_DESCRIPTION', 'My first order');
     
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
     

    Order payment

    Order payment includes payment for all partial payments. Only in this case is considered fully paid.

    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    $paymentCollection = $order->getPaymentCollection();
    foreach ($paymentCollection as $payment)
    {
    	$r = $payment->setPaid('Y');
    	if (!$r->isSuccess())
    	{
    		var_dump($r->getErrorMessages());
    	}
    }
     
    
    if (!$r->isSuccess())
    { 
    	var_dump($r->getErrorMessages());
    }

    Allowing order shipment

    Allowed delivery permits all included partial shipments. Only in this case order is considered as fully paid.

     
    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    $shipmentmentCollection = $order->getShipmentCollection();
    foreach ($shipmentmentCollection as $shipment)
    {
    	$r = $shipment->allowDelivery();
    	if (!$r->isSuccess())
    	{
    		var_dump($r->getErrorMessages());
    	}
    }
    
    $r = $order->save();
    if (!$r->isSuccess())
    { 
    	var_dump($r->getErrorMessages());
    }

    When shipment permission must be cancelled, it's sufficient to call $shipment->disallowDelivery() instead of $shipment->allowDelivery() :

     
    
    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    $shipmentCollection = $order->getShipmentCollection();
    foreach ($shipmentCollection as $shipment)
    {
    	$r = $shipment->disallowDelivery();
    	if (!$r->isSuccess())
    	{
    		var_dump($r->getErrorMessages());
    	}
    }
     
    $r = $order->save();
    if (!$r->isSuccess())
    { 
    	 var_dump($r->getErrorMessages());
    }

    Adding an order comment

     
    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    
    $r = $order->setField('COMMENTS', 'Comment');
    if (!$r->isSuccess())
    {
    	var_dump($r->getErrorMessages());
    }
     
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
    

    Updating order status

     
    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    
    $r = $order->setField('STATUS_ID', 'P');
    if (!$r->isSuccess())
    {
    	var_dump($r->getErrorMessages());
    }
     
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
    

    Cancelling order

    To cancel the order, all payments must be unpaid and shipments non-delivered. Otherwise, error is printed. That's why, before cancelling an order, shipment and payment must be cancelled beforehand.

     
    
    $order = \Bitrix\Sale\Order::load(123); // 123 - order ID
    
    $r = $order->setField('CANCELED', 'Y');
    if (!$r->isSuccess())
    {
    	var_dump($r->getErrorMessages());
    }
     
    $r = $order->save();
    if (!$r->isSuccess())
    { 
        var_dump($r->getErrorMessages());
    }
    

    Deleting an order

    There are 2 approaches to deleting an order: "smart" and "straightforward".

    • In case of "smart" deleting, the order is cancelled first and then deleted. It means that deleted order cannot have successful payments and shipped deliveries.
       
      $r = \Bitrix\Sale\Order::delete(123); // 123 - order ID
      if (!$r->isSuccess())
      {
      	var_dump($r->getErrorMessages());
      }
      
    • When deleting using "straightforward" approach, order will not be cancelled. Order information is directly deleted from the database.
      $r = \Bitrix\Sale\OrderBase::deleteNoDemand(123); // 123 - order ID
      if (!$r->isSuccess())
      {
      	var_dump($r->getErrorMessages());
      }

    Getting public link to order

    It's necessary for the Online Store module seeings to have the selected option "View order using key without logging in"(Settings > System settings > Module Settings > Online Store).

    
    if (\Bitrix\Sale\Helpers\Order::isAllowGuestView($order))
    {
    	$publicLink = \Bitrix\Sale\Helpers\Order::getPublicLink($order);
    }
    

    Selecting order by property value

    $dbRes = \Bitrix\Sale\Order::getList([
        'select' => ['ID'],
      'filter' => [
          'PROPERTY.ORDER_PROPS_ID' => 15,// 15 - property ID
          'PROPERTY.VALUE' => 'value',
      ],
      'order' => ['ID' => 'DESC']
    ]); 


    © «Bitrix24», 2001-2024
    Up