P
P
Philipp R.2018-05-29 13:28:28
1C-Bitrix
Philipp R., 2018-05-29 13:28:28

Find out the last order ID of a user?

Hello.
In this piece, the first order made by the user is displayed, and I need to get the ID of the last order generated. please help.

$arFilter = Array(
        "USER_ID" => $USER->GetID(),
    );
    $db_sales = CSaleOrder::GetList(array(), $arFilter);
    while ($ar_sales = $db_sales->Fetch())
    {
        $lasorderid = $ar_sales['ID']; //присвоили переменной ID заказа
    break; //оборвали цикл
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2018-05-29
@RFV_online

this is of course govnokod, tk. you need to edit the query to the database, and there already set the condition

$arFilter = Array(
    "USER_ID" => $USER->GetID(),
);
$db_sales = CSaleOrder::GetList(array(), $arFilter);
$ids = [];
while ($ar_sales = $db_sales->Fetch())
{
    $ids[] = $ar_sales['ID']; // сохраняем все id
}
$orderId =  $ids[count($ids)-1]; // последний элемент массива

A
ArmBar, 2018-06-01
@ArmBar

So it will still be better, a request with limit is faster than without it

$orderData = \Bitrix\Sale\Order::getList([
  'select' => ['ID'],
  'filter' => ['=USER_ID' => $USER->GetID()],
  'order' => ['ID' => 'DESC'],
  'limit' => 1
]);
if ($order = $orderData->fetch())
{
  $lastOrderId = $order['ID'];
}

O
Oleg, 2018-05-29
@402d

It is more correct to deal with the CSaleOrder class.
the first parameter of GetList() is what ?
and there is definitely no 3rd or 4th parameter?
Whether in a method to check sorting is explicitly specified?
and the fact that the goods are returned from the first to the last is just an accident.
But this is if you don’t find through the above questions how
to force the method to return only one required order

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question