Views: 13216
Last Modified: 23.03.2023

Do not forget to connect sale module.

Types of locations

Adding a location type:

$res = \Bitrix\Sale\Location\TypeTable::add(array(
	'CODE' => 'CITY',
	'SORT' => '100', // nesting level
	'DISPLAY_SORT' => '200', // display priority when searching
	'NAME' => array( // language names
		),
		'en' => array(
			'NAME' => 'City'
		),
	)
));
if($res->isSuccess())
{
	print('Type added with ID = '.$res->getId());
}

Location type update

$res = \Bitrix\Sale\Location\TypeTable::update(21, array(
	'SORT' => '300',
	'NAME' => array(
		'en' => array(
			'NAME' => 'New City'
		),
	)
));
if($res->isSuccess())
{
	print('Updated!');
}

Deleting location type

$res = \Bitrix\Sale\Location\TypeTable::delete(21);
if($res->isSuccess())
{
	print('Deleted!');
}

Getting type of location by ID

$item = \Bitrix\Sale\Location\TypeTable::getById(14)->fetch();
print_r($item);

Getting list of types with names on the current language

$res = \Bitrix\Sale\Location\TypeTable::getList(array(
	'select' => array('*', 'NAME_EN' => 'NAME.NAME'),
	'filter' => array('=NAME.LANGUAGE_ID' => LANGUAGE_ID)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting groups with account of hierarchy having this location

<?
\Bitrix\Main\Loader::includeModule('sale');

function getGroupsByLocation($locationId)
{
    $res = \Bitrix\Sale\Location\LocationTable::getList([
        'filter' => ['=ID' => $locationId],
        'select' => [
            'ID', 'LEFT_MARGIN', 'RIGHT_MARGIN'
        ]
    ]);

    if(!$loc = $res->fetch())
    {
        return [];
    }

    $locations = [$locationId];

    $res = \Bitrix\Sale\Location\LocationTable::getList([
        'filter' => [
            '<LEFT_MARGIN' => $loc['LEFT_MARGIN'],
            '>RIGHT_MARGIN' => $loc['RIGHT_MARGIN'],
            'NAME.LANGUAGE_ID' => LANGUAGE_ID,
        ],
        'select' => [
            'ID',
            'LOCATION_NAME' => 'NAME.NAME'
        ]
    ]);

    while($locParent = $res->fetch())
    {
        $locations[] = $locParent['ID'];
    }

    $res = \Bitrix\Sale\Location\GroupLocationTable::getList([
        'filter' => ['=LOCATION_ID' => $locations]
    ]);

    $groups = [];

    while($groupLocation = $res->fetch())
    {
        $groups[] = $groupLocation['LOCATION_GROUP_ID'];
    }

    return $groups;
}

Locations

Adding

$res = \Bitrix\Sale\Location\LocationTable::add(array(
	'CODE' => 'newly-created-location-code',
	'SORT' => '100', // priority for showing in list
	'PARENT_ID' => 1, // parent location ID
	'TYPE_ID' => 14, // type ID
	'NAME' => array( // language names
		'en' => array(
			'NAME' => 'New York'
		),
	),
	'EXTERNAL' => array( // external service values
		array(
			'SERVICE_ID' => 1, // service ID
			'XML_ID' => '163000' // value
		),
		array(
			'SERVICE_ID' => 1,
			'XML_ID' => '163061'
		),
	)
));
if($res->isSuccess())
{
	print('Location added with ID = '.$res->getId());
}
else
{
	print_r($res->getErrorMessages());
}

Update

$res = \Bitrix\Sale\Location\LocationTable::update(3156, array(
	'PARENT_ID' => 33,
	'NAME' => array(
		'de' => array(
			'NAME' => 'New York'
		),
	)
));
if($res->isSuccess())
{
	print('Updated!');
}

Deleting

$res = \Bitrix\Sale\Location\LocationTable::delete(3156);
if($res->isSuccess())
{
	print('Deleted!');
}

Getting location by ID

$item = \Bitrix\Sale\Location\LocationTable::getById(3159)->fetch();
print_r($item);

Getting location by CODE, with optional filtering\field retrieval. In fact, this is a wrapper \Bitrix\Sale\Location\LocationTable::getList().

$item = \Bitrix\Sale\Location\LocationTable::getByCode('newly-created-location-code', array(
	'filter' => array('=NAME.LANGUAGE_ID' => LANGUAGE_ID),
	'select' => array('*', 'NAME_EN' => 'NAME.NAME')
))->fetch();
print_r($item);

Getting list of locations with names on the current language and type codes

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array('=NAME.LANGUAGE_ID' => LANGUAGE_ID),
	'select' => array('*', 'NAME_EN' => 'NAME.NAME', 'TYPE_CODE' => 'TYPE.CODE')
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting node direct descendants with ID=1 with names on the current language, codes and names for location types

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array(
		'=ID' => 1, 
		'=CHILDREN.NAME.LANGUAGE_ID' => LANGUAGE_ID,
		'=CHILDREN.TYPE.NAME.LANGUAGE_ID' => LANGUAGE_ID,
	),
	'select' => array(
		'CHILDREN.*',
		'NAME_EN' => 'CHILDREN.NAME.NAME',
		'TYPE_CODE' => 'CHILDREN.TYPE.CODE',
		'TYPE_NAME_EN' => 'CHILDREN.TYPE.NAME.NAME'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting parent nodes for three nodes

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array(
		'=ID' => array(3159, 85, 17), 
		'=PARENT.NAME.LANGUAGE_ID' => LANGUAGE_ID,
		'=PARENT.TYPE.NAME.LANGUAGE_ID' => LANGUAGE_ID,
	),
	'select' => array(
		'PARENT.*',
		'NAME_EN' => 'PARENT.NAME.NAME',
		'TYPE_CODE' => 'PARENT.TYPE.CODE',
		'TYPE_NAME_EN' => 'PARENT.TYPE.NAME.NAME'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting path from tree root to the current item

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array(
		'=ID' => 224, 
		'=PARENTS.NAME.LANGUAGE_ID' => LANGUAGE_ID,
		'=PARENTS.TYPE.NAME.LANGUAGE_ID' => LANGUAGE_ID,
	),
	'select' => array(
		'I_ID' => 'PARENTS.ID',
		'I_NAME_EN' => 'PARENTS.NAME.NAME',
		'I_TYPE_CODE' => 'PARENTS.TYPE.CODE',
		'I_TYPE_NAME_EN' => 'PARENTS.TYPE.NAME.NAME'
	),
	'order' => array(
		'PARENTS.DEPTH_LEVEL' => 'asc'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting list of root nodes with indicated number of descendants

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array(
		'=PARENT_ID' => 0,
		'=NAME.LANGUAGE_ID' => LANGUAGE_ID,
		'=TYPE.NAME.LANGUAGE_ID' => LANGUAGE_ID,
	),
	'select' => array(
		'ID',
		'NAME_EN' => 'NAME.NAME',
		'TYPE_CODE' => 'TYPE.CODE',
		'TYPE_NAME_EN' => 'TYPE.NAME.NAME',
		'CHILD_CNT'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting external data for locations with indicated service code

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'filter' => array(
		'CODE' => array('newly-created-location-code', '0000028090'),
	),
	'select' => array(
		'EXTERNAL.*',
		'EXTERNAL.SERVICE.CODE'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting subtree of nodes on the current language

$res = \Bitrix\Sale\Location\LocationTable::getList(array(
	'runtime' => array(
		'SUB' => array(
			'data_type' => '\Bitrix\Sale\Location\Location',
			'reference' => array(
				'>=ref.LEFT_MARGIN' => 'this.LEFT_MARGIN',
				'<=ref.RIGHT_MARGIN' => 'this.RIGHT_MARGIN'
			),
			'join_type' => "inner"
		)
	),
	'filter' => array(
		'=CODE' => '0000028042',
		'=SUB.NAME.LANGUAGE_ID' => LANGUAGE_ID
	),
	'select' => array(
		'S_CODE' => 'SUB.CODE',
		'S_NAME_EN' => 'SUB.NAME.NAME',
		'S_TYPE_CODE' => 'SUB.TYPE.CODE'
	)
));
while($item = $res->fetch())
{
	print_r($item);
}

Getting locations included into the group without hierarchy.

\Bitrix\Main\Loader::includeModule('sale');


/* Group identifier */
$groupId = 1

/* Get locations included into group */
$res = \Bitrix\Sale\Location\GroupLocationTable::getConnectedLocations(1);

while($item = $res->fetch())
{
    var_dump($item);
}



Courses developed by Bitrix24