require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
use Bitrix\Sale;
CModule::IncludeModule('iblock');
$type = $_POST["type"];
if($type == "sections"){
GetSections();
}
if($type == "items"){
GetItems($_POST["section_id"]);
}
if($type == "add"){
addItemToOrder($_POST["orderID"], $_POST["itemID"], $_POST["orderName"]);
}
function GetSections(){
$arFilter = array('IBLOCK_ID' => 11, 'ACTIVE' => 'Y');
$rsSection = CIBlockSection::GetTreeList($arFilter);
$sections = "
";
while($arSection = $rsSection->Fetch()){
$depth = $arSection["DEPTH_LEVEL"];
if($depth == 1){
$sections .= "
" . $arSection["NAME"] . "
";
}elseif($depth == 2){
$d = $depth;
$sections .= "
" . $arSection["NAME"] . "
";
}else{
$d = $depth + 1;
$sections .= "
" . $arSection["NAME"] . "
";
}
}
$sections .= "
";
echo $sections;
}
function GetItems($id){
$arFilter = array("IBLOCK_ID" => 11, "ACTIVE" => "Y", "SECTION_ID" => $id);
$arItems = CIBlockElement::GetList(array("SORT" => "ASC"), $arFilter, false, false, array("ID", "XML_ID", "NAME", "PREVIEW_PICTURE"));
while($item = $arItems->GetNext()){
$arResult[] = $item;
};
// echo "";
foreach($arResult as $key => $item){
$price = CPrice::GetBasePrice($item["ID"]);
$image = CFile::GetPath($item["PREVIEW_PICTURE"]);
$data .= "
" . CurrencyFormat($price["PRICE"], "RUB") . "
Добавить
";
}
$data .= "
";
}else{
$data = "Список товаров пуст.
";
}
echo $data;
}
function addItemToOrder($orderID, $itemID, $orderName){
CModule::IncludeModule('sale');
$order = Sale\Order::load($orderID);
$basket = $order->getBasket();
if ($item = $basket->getExistsItem('catalog', $itemID)) {
$item->setField('QUANTITY', $item->getQuantity() + 1);
}else{
$item = $basket->createItem('catalog', $itemID);
$item->setFields(array(
'QUANTITY' => 1,
'CURRENCY' => "RUB",
'NAME' => $orderName,
'LID' => Bitrix\Main\Context::getCurrent()->getSite(),
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
));
}
$order->save();
echo "Добавлено";
}
?>