AddEventHandler("sale", "OnSalePayOrder", "UserDiscount");
function UserDiscount($id,$val)
{
$UserDiscount= new CUser;
$arFields = Array();
$arOrder=CSaleOrder::GetByID($id);
$rsUsers = $UserDiscount->GetList(($by="personal_country"), ($order="desc"), Array('USER_ID'=>$arOrder['USER_ID']),array('SELECT'=>Array('UF_BONUS')));
$arUSER=$rsUsers->Fetch();
$bonus = 0;
$dbBasketItems = CSaleBasket::GetList(
array(
"NAME" => "ASC",
"ID" => "ASC"
),
array(
"LID" => SITE_ID,
"ORDER_ID" => $id
),
false,
false,
array("PRODUCT_ID", "QUANTITY")
);
while ($arItems = $dbBasketItems->Fetch())
{
mail("nastudio@ya.ru", "My Subject", $arItems["PRODUCT_ID"]);
$db_props = CIBlockElement::GetProperty(29, $arItems["PRODUCT_ID"], "sort", "asc", Array("CODE"=>"BONUS"));
if($ar_props = $db_props->Fetch()){
$bonus = ($ar_props["VALUE"]*$arItems["QUANTITY"])+$bonus;
}
}
if ($val=='Y'){
$arFields['UF_BONUS'] = Double Val($arUSER['UF_BONUS'])+$bonus;
}elseif(($val=='N')&&(Double Val($arUSER['UF_BONUS'])>0)){
$arFields['UF_BONUS']=Double Val($arUSER['UF_BONUS'])-$bonus;
}
$UserDiscount->Update($arOrder['USER_ID'],$arFields);
} |