Answer the question
In order to leave comments, you need to log in
How to get data in the onSaleDeliveryServiceCalculate event?
There is such task:
For example, the buyer forms the order on a site from several positions. Further restrictions are triggered, a list of deliveries available for the current choice of the buyer is created. If all the goods from the order are in the warehouse tied to delivery - everything is OK, we happily run to pay. But if not, you need to add some rubles to the delivery cost for each item in the order that is not in the selected warehouse. At the same time, you need to recalculate all delivery services immediately when you get to the checkout page so that the current figure is next to them.
OnSaleComponentOrderCreated->$arDeliveryServiceAll - doesn't work for some reason,
$order->getShipmentCollection - gives some nonsense. The thought came that until he does save, everything will lie somewhere in Ajax. After all, when loading the page, while Ajax has not calculated everything in the frame, there is no information there?
I tried to make a function in init.php for the onSaleDeliveryServiceCalculate event - but then this function works 20 times, given that there are a lot of deliveries and, apparently, it does not take into account the restrictions in this calculation. I also have a self-delivery warehouse attached to each delivery service, I did not find how to get at least the ID of this warehouse.
AddEventHandler("sale", "onSaleDeliveryServiceCalculate", "setDelivery");
function setDelivery($result, $shipment, $deliveryID) {
$deliveryList = \Bitrix\Sale\Delivery\Services\Manager::getRestrictedObjectsList($shipment); // so it happened to get a list of deliveries with restrictions, the documentation does not say anything about this
foreach ($deliveryList as $delivery) {
$id[] = $delivery->getId(); // list of delivery IDs available in the order
}
if (in_array($deliveryID, $id)) { // compare the contents of the argument with the previously created array
$deliveryPrice = $result->getDeliveryPrice();
if ($deliveryPrice > 0) {
$newValue = $deliveryPrice + some money; // if you manage to display this option in the admin panel, it will be great
$result->setDeliveryPrice($newValue); // write new value
}
}
}
That's all, then a dead end. I don’t understand how to get some data from shipmentcollection inside onSaleDeliveryServiceCalculate , the event fires before others on the page. It is necessary to obtain data on linked warehouses for available deliveries in order to check the availability of goods there - it is also not clear how.
Help, experienced comrades.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question