Answer the question
In order to leave comments, you need to log in
How to add a product to an existing order?
Hello.
Suggest how to add a product to an already created order? I have functionality for a specific group of users who make orders. They have their own admin who checks orders. That is, he sees a list of all orders and the goods in them (this is all implemented on the front). It is necessary to make it possible to add a new product to any order.
Answer the question
In order to leave comments, you need to log in
1) You need to add the product to the order basket:
//$products массив id товаров, которые надо добавить
$basket = $order->getBasket();
$quantity = 1;
foreach ($products as $productId) {
$item = $basket->createItem('catalog', $productId);
$item->setFields(array(
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'LID' => \Bitrix\Main\Context::getCurrent()->getSite(),
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
));
$item->setField("QUANTITY", quantity);
$newBasketItems[] = $item;
}
//В этом примере товары добавляются в первую попавшуюся НЕ СИСТЕМНУЮ отгрузку.
$shipmentCollection = $order->getShipmentCollection();
foreach ($shipmentCollection as $shipment) {
if (!$shipment->isSystem()) {
foreach ($newBasketItems as $newBasketItem) {
/** @var \Bitrix\Sale\Shipment $shipment */
$shipmentItemCollection = $shipment->getShipmentItemCollection();
$shipmentItem = $shipmentItemCollection->createItem($newBasketItem);
$shipmentItem->setQuantity($item->getQuantity());
}
break;
}
}
$discount = $order->getDiscount();
\Bitrix\Sale\DiscountCouponsManager::clearApply(true);
\Bitrix\Sale\DiscountCouponsManager::useSavedCouponsForApply(true);
$discount->setOrderRefresh(true);
$discount->setApplyResult(array());
/** @var \Bitrix\Sale\Basket $basket */
if (!($basket = $order->getBasket())) {
throw new \Bitrix\Main\ObjectNotFoundException('Entity "Basket" not found');
}
$basket->refreshData(array('PRICE', 'COUPONS'));
$discount->calculate();
$order->save();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question