V
V
v_i_rus2021-07-19 11:49:55
1C-Bitrix
v_i_rus, 2021-07-19 11:49:55

Bitrix: why are goods not saved in shipments?

The task is to create two shipments upon receipt of an order, each with 50% of the goods. Everything happens in the custom component order.d7. Shipments are created, and, immediately after the method call, you can even withdraw goods from them, but after placing an order, two empty shipments remain without any goods at all. What could be the problem?

private function addShipment() {
        if (count($this->basketItems) > 1) {
            /* @var $shipmentCollection ShipmentCollection */
            $shipmentCollection = $this->order->getShipmentCollection();
            /* @var $shipment Shipment */

            foreach ($shipmentCollection as $key => $shipment) {//убрать системную отгрузку. должна остаться одна.
                if ($shipment->isSystem()) {
                    unset($shipmentCollection[$key]);
                }
            }
            unset($shipment);

            //получить данные оставшейся отгрузки, для записи в последующие
            $shipmentCollection->rewind();
            $sysShipment = $shipmentCollection->current();

            /* @var $firstShipment Shipment */
            $firstShipment = $shipmentCollection->createItem();

            /** @var Bitrix\Sale\ShipmentItemCollection $firstItemCollection */
            $firstItemCollection = $firstShipment->getShipmentItemCollection();

            //записываем в первую отгрузку половину позиций
            $halfCount = ceil($this->basketItems->count() / 2);
            $this->basketItems->rewind();
            for ($i = 0; $i < $halfCount; $i++) {
                if ($i == $halfCount) break;

                /** @var \Bitrix\Sale\BasketItem $basketItem */
                $basketItem = $this->basketItems->current();

                /** @var Bitrix\Sale\ShipmentItem $shipmentItem */
                $shipmentItem = $firstItemCollection->createItem($basketItem);
                $shipmentItem->setQuantity($basketItem->getQuantity());

                $this->basketItems->next();
            }

            unset($shipmentItem);

            $firstShipment->setFields([
                'DELIVERY_ID' => !is_null($sysShipment->getDeliveryId()) ? $sysShipment->getDeliveryId() : 0,
                'DELIVERY_NAME' => $sysShipment->getDeliveryName(),
                'CURRENCY' => $sysShipment->getCurrency(),
            ]);

            try {
                $firstShipment->setFields([
                    'PRICE_DELIVERY' => Manager::calculateDeliveryPrice($firstShipment, (!is_null($sysShipment->getDeliveryId())) ? (int)$sysShipment->getDeliveryId() : 0)
                ]);
            } catch (\Bitrix\Main\ArgumentNullException $e) {}

            $secondShipment = $shipmentCollection->createItem();

            /** @var Bitrix\Sale\ShipmentItemCollection $secondItemCollection */
            $secondItemCollection = $secondShipment->getShipmentItemCollection();

            //записываем во вторую отгрузку вторую половину позиций
            for ($i = 0; $i < $this->basketItems; $i++) {
                /** @var \Bitrix\Sale\BasketItem $basketItem */
                $basketItem = $this->basketItems->current();

                /** @var Bitrix\Sale\ShipmentItem $shipmentItem */
                $shipmentItem = $secondItemCollection->createItem($basketItem);
                $shipmentItem->setQuantity($basketItem->getQuantity());

                $this->basketItems->next();
            }

            $secondShipment->setFields([
                'DELIVERY_ID' => !is_null($sysShipment->getDeliveryId())?$sysShipment->getDeliveryId(): 0,
                'DELIVERY_NAME' => $sysShipment->getDeliveryName(),
                'CURRENCY' => $sysShipment->getCurrency(),
            ]);

            try {
                $secondShipment->setFields([
                    'PRICE_DELIVERY' => Manager::calculateDeliveryPrice($secondShipment, (!is_null($sysShipment->getDeliveryId())) ? (int)$sysShipment->getDeliveryId() : 0)
                ]);
            } catch (\Bitrix\Main\ArgumentNullException $e) {}

            //оригинальную отгрузку надо убрать
            $sysShipment->delete();
        }
    }


I remove goods from shipments after calling the method like this
/* @var $shipmentCollection ShipmentCollection */
$shipmentCollection = $this->order->getShipmentCollection();
foreach ($shipmentCollection as $shipment){
    /** @var Bitrix\Sale\Shipment $shipment */
    if(!$shipment->isSystem()) {
        $ItemCollection = $shipment->getShipmentItemCollection()->getSellableItems();
        foreach ($ItemCollection as $item) {
            /** @var Bitrix\Sale\ShipmentItem $item */
            var_dump($item->getProductId());
        }
    }
}


I just got acquainted with shipments, so most likely I don’t see an obvious mistake.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v_i_rus, 2021-07-26
@v_i_rus

For some reason, products were not added if the order had already been created. If the same actions are performed at the time of creating the order, then everything works as it should.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question