Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. <?
  2. AddEventHandler("iblock", "OnAfterIBlockElementAdd", "OnIBlockElementAdd");
  3. AddEventHandler("iblock", "OnBeforeIBlockElementUpdate", "OnIBlockElementUpdate");
  4. CModule::IncludeModule("iblock");
  5.  
  6. function OnIBlockElementAdd(&$arFields)
  7. {
  8. if($arFields['IBLOCK_ID'] == 10){
  9.  
  10. $average_rating = 0;
  11. $count = 0;
  12. $is_active = false;
  13. $res = CIBlockElement::GetList(array(), array('IBLOCK_ID' => 10, 'ACTIVE' => 'Y', 'PROPERTY_PRODUCT_ID'=> $arFields['PROPERTY_VALUES']['PRODUCT_ID']), false, false, array("ID", 'PROPERTY_RATING'));
  14. while($ar = $res->GetNext()){
  15. $is_active = true;
  16. $count++;
  17. $average_rating += $ar['PROPERTY_RATING_VALUE'];
  18. }
  19. $average_rating = round($average_rating / $count);
  20.  
  21. if($is_active){
  22. CIBlockElement::SetPropertyValuesEx($arFields['PROPERTY_VALUES']['PRODUCT_ID'], 2, array('COMMENTS_CNT' => $count, 'rating' => $average_rating));
  23. }
  24.  
  25. }
  26. }
  27.  
  28. function OnIBlockElementUpdate(&$arFields)
  29. {
  30. if( ( $arFields['IBLOCK_ID'] == 2 && !CCatalogSKU::IsExistOffers($arFields['ID'],$arFields['IBLOCK_ID']) ) || $arFields['IBLOCK_ID'] == 3){
  31.  
  32. $ar_res = CCatalogProduct::GetByID($arFields['ID']);
  33.  
  34. if($ar_res['QUANTITY'] == 0){
  35.  
  36. $not_available = ($arFields['IBLOCK_ID'] == '3') ? '33': '35';
  37. $porduct_state = ($arFields['IBLOCK_ID'] == '3') ? '92': '60';
  38.  
  39.  
  40. $arFields['PROPERTY_VALUES'][$porduct_state] = [ 0 => ["VALUE" => $not_available] ];
  41.  
  42. }
  43. }
  44.  
  45. if($arFields['IBLOCK_ID'] == 2){
  46.  
  47. if($arFields['PROPERTY_VALUES'][60][0]['VALUE'] == '22' || $arFields['PROPERTY_VALUES'][60][0]['VALUE'] == '23'){
  48.  
  49. $arFields['PROPERTY_VALUES'][59] = [ 0 => ["VALUE" => 20] ];
  50.  
  51. }
  52.  
  53. }
  54.  
  55. if($arFields['IBLOCK_ID'] == 10){
  56.  
  57. $average_rating = 0;
  58. $count = 0;
  59. $is_active = false;
  60.  
  61. $res = CIBlockElement::GetList(array(), array('IBLOCK_ID' => 10, 'ACTIVE' => 'Y', 'PROPERTY_PRODUCT_ID'=> $arFields['PROPERTY_VALUES']['81']['5061']['VALUE']), false, false, array("ID", 'PROPERTY_RATING'));
  62. while($ar = $res->GetNext()){
  63. $is_active = true;
  64. $count++;
  65. $average_rating += $ar['PROPERTY_RATING_VALUE'];
  66. }
  67. $average_rating = round($average_rating / $count);
  68.  
  69. if($is_active){
  70. CIBlockElement::SetPropertyValuesEx($arFields['PROPERTY_VALUES']['81']['5061']['VALUE'], 2, array('COMMENTS_CNT' => $count, 'rating' => $average_rating));
  71. }
  72.  
  73. }
  74.  
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement