namespace Sale Handlers Delivery use Bitrix Sale Delivery CalculationR

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?
namespace Sale\Handlers\Delivery;
use Bitrix\Sale\Delivery\CalculationResult;
use Bitrix\Sale\Delivery\Services\Base;
class KmdistanceHandler extends Base
{
public static function getClassTitle()
{
return 'Доставка по километражу';
}
public static function getClassDescription()
{
return 'Доставка, стоимость которой зависит от расстояния';
}
protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment)
{
$result = new CalculationResult();
$price = floatval($this->config["MAIN"]["PRICE"]);
$order = $shipment->getCollection()->getOrder();
$props = $order->getPropertyCollection();
$distance = $props->getItemByOrderPropertyId(21);
// формат 10:105;20:200;30:450 $this->config['MAIN']['PRICE_KM']
$prices = explode(';', $this->config['MAIN']['PRICE_KM']);
foreach ($prices as $pricePairStr) {
$pricePair = explode(':', $pricePairStr);
$resultPrice[$pricePair[0]] = $pricePair[1];
}
ksort($resultPrice);
if ($distance->getValue() == 0) {
$price = 0; // если дистанция не найдена яндексом
} else {
foreach ($resultPrice as $key => $value) {
if($distance->getValue() < $key) {
$price = $value;
break;
}
}
}
$result->setDeliveryPrice(roundEx($price, 2));
return $result;
}
protected function getConfigStructure()
{
return array(
"MAIN" => array(
"TITLE" => 'Настройка обработчика',
"DESCRIPTION" => 'Настройка обработчика',
"ITEMS" => array(
"PRICE_KM" => array(
"TYPE" => "STRING",
"MIN" => 0,
"NAME" => 'Стоимость доставки (маска 10:150;20:200;30:450)'
),
)
)
);
}
public function isCalculatePriceImmediately()
{
return true;
}
public static function whetherAdminExtraServicesShow()
{
return true;
}
}
?>