Working with warehouses and quantity of goods in Bitrix D7
The last notes
All English-language materials have been translated fully automatically using the Google service
Selecting a warehouse by ID in Bitrix via D7
$arStore = \Bitrix\Catalog\StoreTable::getById($ID)->fetch();
Selection of active warehouses:
// Selection of all active warehouses:
$rsStore = \Bitrix\Catalog\StoreTable::getList(array(
'filter' => array('ACTIVE'>='Y'),
));
while($arStore=$rsStore->fetch()){
$arIBlockListID['STORES'][$arStore['ID']]=$arStore;
}
To select all fields, including custom ones, add to the query the construction 'select' => array ('*', 'UF_ *')
:
$arStore = \Bitrix\Catalog\StoreTable::getList(array(
'filter'=>array('=ID'=>$storeId),
'select'=>array('*','UF_*'),
))->fetch();
Add caching to the selection of warehouses
CModule::IncludeModule("catalog");
$cntIBLOCK_List = 'Stores';
$cache = new CPHPCache();
$cache_time = 3600*24*360; //кеш на год
$cache_id = 'arIBlockListID'.$cntIBLOCK_List;
$cache_path = 'arIBlockListID';
if ($cache_time > 0 && $cache->InitCache($cache_time, $cache_id, $cache_path))
{
$res = $cache->GetVars();
if (is_array($res["arIBlockListID"]) && (count($res["arIBlockListID"]) > 0)){
$arIBlockListID = $res["arIBlockListID"];
}
$arIBlockListID['CACHE']='true';
}
if (!is_array($arIBlockListID))
{
$arIBlockListID['CACHE']='false';
//Выборка всех активных складов:
$rsStore = \Bitrix\Catalog\StoreTable::getList(array(
'filter' => array('ACTIVE'>='Y'),
));
while($arStore=$rsStore->fetch()){
$arIBlockListID['STORES'][$arStore['ID']]=$arStore;
}
//////////// end cache /////////
if ($cache_time > 0)
{
$cache->StartDataCache($cache_time, $cache_id, $cache_path);
$cache->EndDataCache(array("arIBlockListID"=>$arIBlockListID));
}
}
If you are using Aspro store, you can use the built-in function:
$arStores=CNextCache::CCatalogStore_GetList(array(), array("ACTIVE" => "Y"), false, false, array());
Get all information about the product, including the quantity (deprecated method):
CCatalogProduct::GetByID($productID)
Fetching the quantity of a product with an identifier in $ productId in all active warehouses:
$rsStoreProduct = \Bitrix\Catalog\StoreProductTable::getList(array(
'filter' => array('=PRODUCT_ID'=>$arResult["ID"],'STORE.ACTIVE'=>'Y'),
'select' => array('*','UF_*'),
));
while($arStoreProduct=$rsStoreProduct->fetch()){
$amount[$arStoreProduct['STORE_ID']] = $arStoreProduct;
}
Selecting the quantity of goods with the identifier in $ productId in all active warehouses (additionally, select the warehouse name):
$rsStoreProduct = \Bitrix\Catalog\StoreProductTable::getList(array(
'filter' => array('=PRODUCT_ID'=>$productId,'STORE.ACTIVE'=>'Y'),
'select' => array('AMOUNT','STORE_ID','STORE_TITLE' => 'STORE.TITLE'),
));
while($arStoreProduct=$rsStoreProduct->fetch()){
print_r($arStoreProduct);
}
Fetching the quantity of goods with an identifier in $ productId in a warehouse with an identifier in $ storeId (additionally select the name of the warehouse and the name of the product):
$rsStoreProduct = \Bitrix\Catalog\StoreProductTable::getList(array(
'filter' => array('=PRODUCT_ID'=>$productId,'=STORE_ID'=>$storeId),
'limit' => 1,
'select' => array('AMOUNT','STORE_TITLE' => 'STORE.TITLE', 'PRODUCT_NAME' => 'PRODUCT.IBLOCK_ELEMENT.NAME'),
));
if($arStoreProduct=$rsStoreProduct->fetch()){
print_r($arStoreProduct);
}
& nbsp;
Site materials used:
Comments