M
M
MrSancho2017-02-28 13:17:07
1C-Bitrix
MrSancho, 2017-02-28 13:17:07

How to change the composition of the shipment in the order?

How can I change the composition of the shipment in the order?
the following code is used

$order = \Bitrix\Sale\Order::load(3007721);
$shipmentCollection = $order->getShipmentCollection();
$basket = $order->getBasket();
/* DELETE THE SHIPPING, when deleted with this method, the shipment disappears */
foreach ($shipmentCollection as $shipment)
{
$shipmentItemCollection = $shipment->getShipmentItemCollection();
foreach ($shipmentItemCollection as $item){
$item->setQuantity(0);
$item->delete();
$item->save();
}
$shipment->delete();
$shipment->save();
}
/* create a shipment for each product
the shipment is created, but without the goods inside joxi.ru/KAxzvMBuMqKkXm
, also after deleting the shipment, there is no way to add the goods anymore, even through the admin panel

*/
foreach ($basket as $basketItem) {
$shipment = $shipmentCollection->createItem();
$shipment->setField('CURRENCY', $order->getCurrency());
$shipment->setField('DELIVERY_ID', 1);
$shipmentItemCollection = $shipment->getShipmentItemCollection();
$shipmentItem = $shipmentItemCollection->createItem($basketItem);
$shipmentItem->setQuantity($basketItem->getQuantity());
}
$order->save();
/* but when deleting a shipment or a product from the shipment from the admin area,
tell me the solution method? what is the problem?
*/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Burlaka, 2017-02-28
@AlexeyGfi

Compare with the code from the core

/**
   * Initialization of shipment object. Filling with basket items.
   *
   * @param Order $order
   * @return Shipment
   * @throws Main\ArgumentTypeException
   * @throws Main\NotSupportedException
   */
  public function initShipment(Order $order)
  {
    $shipmentCollection = $order->getShipmentCollection();
    $shipment = $shipmentCollection->createItem();
    $shipmentItemCollection = $shipment->getShipmentItemCollection();
    $shipment->setField('CURRENCY', $order->getCurrency());

    /** @var Sale\BasketItem $item */
    foreach ($order->getBasket() as $item)
    {
      /** @var Sale\ShipmentItem $shipmentItem */
      $shipmentItem = $shipmentItemCollection->createItem($item);
      $shipmentItem->setQuantity($item->getQuantity());
    }

    return $shipment;
  }

T
tgarl, 2019-06-05
@tgarl

I understand that the topic is old, but in the end what is the solution?

//собираем id которые нужно снести с корзины
foreach ($basket as $basketItem) {  
$zapjmnimID[] = $basketItem->getField('ID');  
}
//добавляем новый товар
$item = $basket->createItem('catalog', $obEl['ID']); //создаём новый товар в корзине
$item->setFields(array(
   'QUANTITY' => 1,
   'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
   'LID' => 's1',
   'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
   "CATALOG_XML_ID" => $obEl['IBLOCK_EXTERNAL_ID'],
   "PRODUCT_XML_ID" => $obEl['XML_ID'],
   'CUSTOM_PRICE'=>'Y',
   'PRICE' => $PlProductInfo['PRICE'],
   'NAME'=>$arParams['NAME']['VALUE']
));
   
$item->getPropertyCollection()->setProperty($arParams);

//удаляем не нужные товары
foreach($zapjmnimID as $delitem){
   $basket->getItemById($delitem)->delete();
}
//сохраняем корзину
$basket->save();

//отгрузка, доставка
$shipmentCollection = $order->getShipmentCollection();
удаляем текущую отгрузку
foreach ($shipmentCollection as $shipment) {
  $shipment->delete();
}
//добавляем доставку под другой тип               
$shipment = $shipmentCollection->createItem();
$service = \Bitrix\Sale\Delivery\Services\Manager::getById(15);
$shipment->setFields(array(
   'DELIVERY_ID' => $service['ID'],
   'DELIVERY_NAME' => $service['NAME'],
));
$shipmentItemCollection = $shipment->getShipmentItemCollection();
//добавляем товары в отгрузку
foreach ($basket as $basketItem)
{
   $item = $shipmentItemCollection->createItem($basketItem);
   $item->setQuantity($basketItem->getQuantity());
}
//сохраняем заказ
$result = $order->save();

here is my code, but the shipment is empty and I can't figure out what exactly is the problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question