File: //home/bk/100/ajax/basket_market.php
<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
include_once $_SERVER["DOCUMENT_ROOT"].SITE_TEMPLATE_PATH."/lang/".LANGUAGE_ID."/ajax.php";
$arResult = array();
try
{
$productId = intVal($_GET["id"]);
$productQty = intVal($_GET["quantity"]);
if($productId == 0) { throw new Exception(GetMessage('PRODUCT_ID_NOT_FOUND'));}
if($productQty <= 0) { $productQty = 1;}
if (!CModule::IncludeModule('iblock')) {
throw new Exception('Module iblock is not installed', 1);
}
if (!CModule::IncludeModule('yenisite.market')) {
throw new Exception('Module yenisite.market is not installed', 1);
}
$res = CIBlockElement::GetByID($productId);
if($el = $res->GetNextElement())
{
$fields = $el->GetFields();
if(!CMarketCatalog::IsCatalog($fields["IBLOCK_ID"])) {
throw new Exception(GetMessage('MARKET_IBLOCK_NOT_CATALOG', array("#IBLOCK_ID#" => $fields["IBLOCK_ID"])));
}
} else {
throw new Exception(GetMessage('PRODUCT_ID_NOT_FOUND'), 1);
}
$arProduct = CMarketCatalogProduct::GetByID($productId, $fields['IBLOCK_ID']);
if ($arProduct['QUANTITY_TRACE'] == 'Y' && $arProduct['CAN_BUY_ZERO'] != 'Y') {
if ($arProduct['QUANTITY'] <= 0) throw new Exception("Product is not available", 1);
if ($arProduct['QUANTITY'] < $productQty) {
$productQty = $arProduct['QUANTITY'];
}
}
$cartId = CMarketBasket::Add($productId, (array)$_REQUEST["prop"], $productQty);
if(!$cartId){
throw new Exception(GetMessage('ADD_TO_CART_ERROR'));
}
$arResult = array('STATUS' => 'OK', 'MESSAGE' => GetMessage('ADD_TO_CART_SUCCESS'));
}
catch (Exception $e) {
$arResult["STATUS"] = 'ERROR';
$arResult["CODE"] = $e->getMessage();
}
if (!defined('BX_UTF')) {
$arResult['MESSAGE'] = iconv('windows-1251', 'utf-8', $arResult['MESSAGE']);
}
echo json_encode($arResult);
?>