PHP code example of uranum / yii2-delivery-calculator

1. Go to this page and download the library: Download uranum/yii2-delivery-calculator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uranum / yii2-delivery-calculator example snippets


'modules'             => [
		'delivery' => [
			'class' => 'uranum\delivery\module\Module',
			'params' => [...],
            'components'             => [
                $code['POST']       => [
                    'class' => 'uranum\delivery\services\PostDelivery',
                ],
                $code['NALOJ']      => [
                    'class' => 'uranum\delivery\services\PostNalojDelivery',
                ],
                $code['CDEK_STORE'] => [
                    'class' => 'uranum\delivery\services\CdekStore',
                ],
                $code['CDEK_DOOR']  => [
                    'class' => 'uranum\delivery\services\CdekDoor',
                ],
                $code['COURIER']    => [
                    'class' => 'uranum\delivery\services\CourierDelivery',
                ],
                $code['PICKUP']     => [
                    'class' => 'uranum\delivery\services\PickupDelivery',
                ],
                $code['ENERGY']     => [
                    'class' => 'uranum\delivery\services\EnergyDelivery',
                ],
                $code['ON_CHOICE']     => [
                    'class' => 'uranum\delivery\services\OnChoiceDelivery',
                ],
            ],
        ],
]

'modules'             => [
		'delivery' => [
			'class' => 'uranum\delivery\module\Module',
		    'params' => [
			    'locationFrom' => 'Новосибирск',        // Город отправки
			    /** Параметры для postcalc (Почта России) */
			    'siteName'     => 'site.ru',            // Название сайта (ОБЯЗАТЕЛЬНЫЙ)
			    'key'          => 'domen-XXXXX',        // ключ для доступа к API (ОБЯЗАТЕЛЬНЫЙ c 25 декабря 2019 года)
			    'email'        => '[email protected]',       // Контактный email. Самый принципиальный параметр для postcalc (ОБЯЗАТЕЛЬНЫЙ)
			    'contactName'  => 'Eugeny_Emelyanov',   // Контактное лицо. Имя_фамилия, только латиница через подчеркивание (НЕобязательный)
			    'insurance'    => 'f',                  // База страховки - полная f или частичная p (НЕобязательный)
			    'round'        => 1,                    // Округление вверх. 0.01 - округление до копеек, 1 - до рублей (НЕобязательный)
			    'pr'           => 0,                    // Наценка в рублях за обработку заказа (НЕобязательный)
			    'pk'           => 0,                    // Наценка в рублях за упаковку одного отправления (НЕобязательный)
			    'encode'       => 'utf-8',              // Кодировка - utf-8 или windows-1251 (НЕобязательный)
			    'sendDate'     => 'now',                // Дата - в формате, который понимает strtotime(), например, '+7days','10.10.2020' (НЕобязательный)
			    'respFormat'   => 'json',               // Формат ответа (html, php, arr, wddx, json, plain) (НЕобязательный)
			    'country'      => 'Ru',                 // Страна (список стран: http://postcalc.ru/countries.php) (НЕобязательный)
			    'servers'      => [
				    //'api.postcalc.ru',                // После тестовых запросов включить "боевой" сервер (ОБЯЗАТЕЛЬНО)
				    'test.postcalc.ru',
			    ],                                      // Список серверов для беcплатной версии (ОБЯЗАТЕЛЬНЫЙ)
			    'httpOptions'  => [
				    'http' => [
					    'header'     => 'Accept-Encoding: gzip',
					    'timeout'    => 5,              // Время ожидания ответа сервера в секундах
					    'user_agent' => 'PostcalcLight_1.04 ' . phpversion(),
				    ],
			    ],                                      // Опции запроса (НЕобязательный)
		    ]
		],
	],

$services    = Yii::$app->getModule('delivery')->getComponents();
$data        = new DeliveryCargoData(659300, 'Бийск', 2000, 1000, 275); // zip, locationTo, cartCost, weight, innerCode (own carrier code)
$resultArray = new DeliveryCalculator($data, $services);
VarDumper::dump($resultArray->calculate());

[
    0 => [
        'name' => 'Почта России'
        'terms' => '9-13 дн.'
        'cost' => '314.60'
        'info' => null
    ]
    1 => [
        'name' => 'Почта России наложенным'
        'terms' => '9-13 дн.'
        'cost' => 487.18
        'info' => null
    ]
    2 => false
    3 => [
        'name' => 'Самовывоз'
        'terms' => false
        'cost' => 0
        'info' => 'Самостоятельно забираете в офисе.'
    ]
]

'components' => [
        // ...
        'field_code_of_new_delivery' => [
            'class' => 'my\extended\class\NewDeliveryService',
        ],
    ],

public function init()
{
    $this->module->setViewPath('@uranum/delivery/module/views');
    \Yii::$app->i18n->translations['module'] = [
        'class'          => 'yii\i18n\PhpMessageSource',
        'sourceLanguage' => 'en-US',
        'basePath'       => '@vendor/uranum/yii2-delivery-calculator/src/module/messages'
    ];
}