Created
January 15, 2024 10:08
-
-
Save yandexmarketforbitrix/70457f453211cbe3f1a09a2067cac84d to your computer and use it in GitHub Desktop.
Пункты выдачи с доступными товарами
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Bitrix\Main; | |
use Bitrix\Sale; | |
use Bitrix\Catalog; | |
$eventManager = Main\EventManager::getInstance(); | |
$eventManager->addEventHandler('sale', 'onSaleDeliveryServiceCalculate', function(Main\Event $event) { | |
/** @var Sale\Delivery\CalculationResult $calculationResult */ | |
/** @var Sale\Shipment $shipment */ | |
/** @var Sale\Order $order */ | |
$calculationResult = $event->getParameter('RESULT'); | |
$shipment = $event->getParameter('SHIPMENT'); | |
$deliveryId = (int)$event->getParameter('DELIVERY_ID'); | |
$order = $shipment->getOrder(); | |
if ($order === null || $deliveryId <= 0 || mb_strpos((string)$order->getField('XML_ID'), 'YAMARKET_') !== 0) { return; } | |
$deliveryService = Sale\Delivery\Services\Manager::getById($deliveryId); | |
$deliveryStores = Sale\Delivery\ExtraServices\Manager::getStoresList($deliveryId); | |
$basketQuantities = []; | |
/** @var Sale\ShipmentItem $shipmentItem */ | |
foreach ($shipment->getShipmentItemCollection() as $shipmentItem) | |
{ | |
$basketItem = $shipmentItem->getBasketItem(); | |
if ($basketItem === null || $basketItem->getQuantity() <= 0) { continue; } | |
$productId = (int)$basketItem->getProductId(); | |
if (!isset($basketQuantities[$productId])) { $basketQuantities[$productId] = 0; } | |
$basketQuantities[$productId] += (float)$basketItem->getQuantity(); | |
} | |
if (!$deliveryService || empty($deliveryStores) || empty($basketQuantities) || !Main\Loader::includeModule('catalog')) { return; } | |
$storeQuantities = []; | |
$queryStoreAmounts = Catalog\StoreProductTable::getList([ | |
'filter' => [ | |
'=PRODUCT_ID' => array_keys($basketQuantities), | |
'=STORE_ID' => $deliveryStores, | |
], | |
'select' => [ 'PRODUCT_ID', 'STORE_ID', 'AMOUNT' ], | |
]); | |
while ($storeAmount = $queryStoreAmounts->fetch()) | |
{ | |
if (!isset($storeQuantities[$storeAmount['STORE_ID']])) { $storeQuantities[$storeAmount['STORE_ID']] = []; } | |
if (!isset($storeQuantities[$storeAmount['STORE_ID']][$storeAmount['PRODUCT_ID']])) { $storeQuantities[$storeAmount['STORE_ID']][$storeAmount['PRODUCT_ID']] = 0; } | |
$storeQuantities[$storeAmount['STORE_ID']][$storeAmount['PRODUCT_ID']] += (float)$storeAmount['AMOUNT']; | |
} | |
$storesAvailability = []; | |
foreach ($deliveryStores as $storeId) | |
{ | |
$isAvailable = true; | |
foreach ($basketQuantities as $productId => $quantity) | |
{ | |
$storeQuantity = $storeQuantities[$storeId][$productId] ?? 0; | |
if ($storeQuantity < $quantity) | |
{ | |
$isAvailable = false; | |
break; | |
} | |
} | |
$storesAvailability[$storeId] = $isAvailable; | |
} | |
$calculationData = (array)$calculationResult->getData(); | |
$calculationData['MARKET_STORES'] = array_keys(array_intersect($storesAvailability, [ | |
mb_stripos($deliveryService['NAME'], 'под заказ') === false, | |
])); | |
$calculationResult->setData($calculationData); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment