P
P
Pavel2018-07-19 19:51:00
1C-Bitrix
Pavel, 2018-07-19 19:51:00

How to bind a coupon to an order in Bitrix?

When placing an order, you need to add each product to your shipment, while all the functionality of discount coupons should be preserved?
The problem is that when a shipment or an element is removed from a shipment in the onSaleComponentOrderCreated event (works with an object and not a created order), the coupon binding to the order flies. those. it is executed but not placed in the order itself.
The onSaleComponentOrderCreated event works in sale.order.ajax, ie. order exists only as an object

/** @var Sale\ShipmentCollection $shipmentCollection */
      $shipmentCollection = $order->getShipmentCollection();
            $service = Sale\Delivery\Services\Manager::getById(Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId());
            /** @var Sale\Shipment $shipment */
            foreach ($shipmentCollection as $shipment)
            {

                if($shipment->isSystem())
                    continue;

                $shipmentItemCollection = $shipment->getShipmentItemCollection();
                $firstProduct = true;
                if($shipmentItemCollection->count() > 1)
                {
                    //Проход по всем товарам отгрузки и добавление новых отгрузок
                    /** @var Sale\ShipmentItem $item */
                    foreach ($shipmentItemCollection as $key=>$item)
                    {
                        $basketItem = $item->getBasketItem();

                        if($basketItem->isBundleChild())
                            continue;

                        if($firstProduct == true)
                        {   //Первый продукт не удаляем из отгрузки
                            $firstProduct = false;
                            continue;
                        }
                        $item->delete();

                        $newShipment = $shipmentCollection->createItem();
                        $newShipmentItemCollection = $newShipment->getShipmentItemCollection();
                        $newShipment->setField('CURRENCY', $order->getCurrency());
                        /** @var Sale\ShipmentItem $shipmentItem */
                        $shipmentItem = $newShipmentItemCollection->createItem($basketItem);
                        $shipmentItem->setQuantity($basketItem->getQuantity());

                        $newShipment->setFields(array(
                            'DELIVERY_ID' => $service['ID'],
                            'DELIVERY_NAME' => $service['NAME'],
                            'CURRENCY' => $order->getCurrency()
                        ));
                    }
                }
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2018-07-23
@CheBurashka3345

The reason for this behavior was found, namely:
1. it is necessary to recalculate the discount and the basket method
2. class Discount method loadShipment

if ($this->isOrderNew())
{
     foreach ($orderShipmentList as $shipment)
    {
           if ($shipment->isSystem())
                 continue;


                 if (!$loadDelivery)
                 {
                          $this->shipment = $shipment;
                          $loadDelivery = true;
                 }
                 else
                 {
                          $result->addError(new Main\Entity\EntityError(
                          Loc::getMessage('BX_SALE_DISCOUNT_ERR_TOO_MANY_SHIPMENT'),
                                  self::ERROR_ID
                          ));


                           return $result;
                 }
   }
}

Which means that a new (created) order with only one shipment will be recalculated.
What Bitrix TP says - 07/23/2018
Based on the information from this appeal, an application was created to the development department.
Implementation deadlines are set by the development department and depend on the current work plan. It is currently not possible to name a timeframe for a decision.
"When executing $shipment->delete(); and applying Coupons in the cart - There is no link to Coupons in the created order."
Application number: 102288

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question