Views: 2651
Last Modified: 09.06.2022

Customization

System mechanisms allow customizing and adding your own delivery services. In this aspect, e-store in core D7 such services are represented by classes. Subsequently, you can use inheritance mechanism. To create your own delivery service you have to create a class - descendant of the base class \Bitrix\Sale\Delivery\Services\Base .

Example of inheriting for delivery services:

class SimpleHandler extends \Bitrix\Sale\Delivery\Services\Base
{
    protected static $isCalculatePriceImmediately = true;
	protected static $whetherAdminExtraServiceShow = true;
	
	/**
	* @param array $initParams
	* @throws \Bitrix\Main\ArgumentTypeException
	*/
    
    public function __construct(array $initParams)
    {
        parent::__construct($initParams);
    }
}

System will search for handler in the following directories:

self::$handlersDirectories = array(
	'LOCAL' = > '/local/php_interface/include/sale_delivery',
	'CUSTOM' = > '/bitrix/php_interface/include/sale_delivery',
	'SYSTEM' = > '/bitrix/modules/sale/handlers/delivery'
)

You can limit the use of delivery service by specific parameters. For example, by limit by maximum weight or size. Apply standard restrictions for this purpose. When you need something specific, create your own types of restrictions.

Additionally, you can use the the event onSaleDeliveryServiceCalculate available for delivery services that can modify delivery price calculation (i. e. you can increase delivery price per 100 units):

EventManager::getInstance()->addEventHandler(
    'sale',
    'onSaleDeliveryServiceCalculate',
    'myCalc'
);

function myCalc(\Bitrix\Main\Event $event)
{
	/** @var Delivery\CalculationResult $baseResult */
	$baseResult = $event->getParameter('RESULT');
	$shipment = $event->getParameter('SHIPMENT');
	
	$price = $baseResult->getDeliveryPrice() + 100;
	$baseResult->setDeliveryPrice($price);
	
	$event->addResult(
		new EventResult(
			EventResult::SUCCESS, array('RESULT' => $baseResult)
		)
	);
}

Important! You need to consider that the site can have both both UTF-8 and CP-1251 encodings. When data exchange is performed with a delivery service, you need to correctly modify the encoding when sending and receiving data. The following method will be helpful for this purpose: \Bitrix\Main\Text\Encoding::convertEncoding().

Note: you can find more details on handling delivery services REST in a separate chapter This section overviews delivery service REST principles:
  • Delivery service handler settings;
  • Delivery service settings;
  • Additional service setup for delivery service;
  • Handling shipment properties;
  • Standard scenario for delivery services REST: handling delivery in sales center.
    Starting point for manager can be payment feature ("Accept
    payment in deal") or creating a delivery activity.


Learn more...
.

Examples of delivery services

Take a look at the delivery service examples below:

  • \Sale\Handlers\Delivery\SimpleHandler (/bitrix/modules/sale/handlers/delivery/simple/handler.php) - simplest example of handlers.
  • \Sale\Handlers\Delivery\SpsrHandler (/bitrix/modules/sale/handlers/delivery/spsr/handler.php) - a little more complex variant with all features of current architecture fully employed.

There is an available mechanism of automatic tracking of dispatch IDs (tracking number) (i. e., as implemented for delivery service Spsr: \Sale\Handlers\Delivery\SpsrTracking).

Recommendations

It's recommended to use an integrated class \Bitrix\Main\Web\HttpClient for querying delivery services instead of third-party extensions. Optimal exchange format - json, due to the option of integrated class \Bitrix\Main\Web\Json.

Often, during data exchange with delivery services, you must send locations IDs. Matching online store location IDs with delivery service IDs - is a non-trivial task. For example, you can use \Sale\Handlers\Delivery\Spsr\Location::mapStepless().

To void creating redundant queries to delivery service, slowing down site operation, it's preferable to cache information, received from delivery services. However, you have to do it accurately to prevent undesirable side effects. Use \Sale\Handlers\Delivery\Spsr\Cache.

In case of errors, it's recommended to maintain a debugging log with records of events, associated with received information from delivery services. For this purpose, use the class \CEventLog.




Courses developed by Bitrix24