Answer the question
In order to leave comments, you need to log in
How to calculate Bitrix shipping cost?
I place an order in Bitrix.
The question arose of calculating the cost of delivery depending on the location.
Made Location Groups, locations themselves and tied them to groups.
Further, according to the precepts of the Internet, I do the following
//Получаем корзину
$basket = Bitrix\Sale\Basket::loadItemsForFUser(Bitrix\Sale\Fuser::getId(), Bitrix\Main\Context::getCurrent()->getSite());
if(count($basket->getQuantityList())==0){
die(json_encode(array('result'=>false)));
}
//Формируем заказ
if($USER->IsAuthorized()){
$user = new CUser();
$id_user = $user->GetID();
$order = Bitrix\Sale\Order::create(SITE_ID, $id_user);
}else{
$id_user = \CSaleUser::GetAnonymousUserID();
$order = Bitrix\Sale\Order::create(SITE_ID, $id_user);
}
$order->setPersonTypeId(1); //Тип поккупателя
$order->setBasket($basket);
$rsPaySystem = \Bitrix\Sale\Internals\PaySystemActionTable::getList(array('filter' => array('ACTIVE'=>'Y','CODE'=>$payment_system_code)));
$payment_system=null;
if($arPaySystem = $rsPaySystem->fetch())
{
$payment_system=$arPaySystem['ID'];
}
//Устанавливаем доп свойства
$propertyCollection = $order->getPropertyCollection();
$delivery_location = getPropertyByCode($propertyCollection, 'ADDR');
$delivery_location->setValue($del_loc_form);
//Устанавливаем доставку
$shipmentCollection = $order->getShipmentCollection();
$shipment = $shipmentCollection->createItem(
Bitrix\Sale\Delivery\Services\Manager::getObjectById($delivery_service)
);
//Отгрузки
$shipmentItemCollection = $shipment->getShipmentItemCollection();
foreach ($basket as $basketItem) {
$item = $shipmentItemCollection->createItem($basketItem);
$item->setQuantity($basketItem->getQuantity());
}
//Оплата
$arPaySystemServiceAll = [];
$paySystemId = ($payment_system!=null)? $payment_system : 1;
$paymentCollection = $order->getPaymentCollection();
$remainingSum = $order->getPrice() - $paymentCollection->getSum();
if ($remainingSum > 0 || $order->getPrice() == 0)
{
$payment = $paymentCollection->createItem();
$payment->setField('SUM', $remainingSum);
$arPaySystemServices = Bitrix\Sale\PaySystem\Manager::getListWithRestrictions($payment);
$arPaySystemServiceAll += $arPaySystemServices;
if (array_key_exists($paySystemId, $arPaySystemServiceAll))
{
$arPaySystem = $arPaySystemServiceAll[$paySystemId];
}
else
{
reset($arPaySystemServiceAll);
$arPaySystem = current($arPaySystemServiceAll);
}
if (!empty($arPaySystem))
{
$payment->setFields(array(
'PAY_SYSTEM_ID' => $arPaySystem["ID"],
'PAY_SYSTEM_NAME' => $arPaySystem["NAME"]
));
}
else
$payment->delete();
}
die(json_encode(array('result'=>true,'data'=>$order->getDeliveryPrice())));
Answer the question
In order to leave comments, you need to log in
The easiest way is to find an analogue of this code in the version of Bitrix used - the example could be out of date.
Read this Bitrix topic. How to specify the delivery address via the API?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question