Exemple #1
0
 /**
  * The function add viewed product
  *
  * @param array $arFields - params for add
  * @return true false
  */
 public static function Add($arFields)
 {
     global $DB;
     global $USER;
     global $APPLICATION;
     foreach (GetModuleEvents("sale", "OnBeforeViewedAdd", true) as $arEvent) {
         if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) {
             return false;
         }
     }
     if (isset($arFields["ID"])) {
         unset($arFields["ID"]);
     }
     $arFields["PRODUCT_ID"] = IntVal($arFields["PRODUCT_ID"]);
     $arFields["USER_ID"] = IntVal($arFields["USER_ID"]);
     $arFields["FUSER_ID"] = IntVal($arFields["FUSER_ID"]);
     $arFields["IBLOCK_ID"] = IntVal($arFields["IBLOCK_ID"]);
     if (strlen($arFields["CALLBACK_FUNC"]) <= 0) {
         $arFields["CALLBACK_FUNC"] = "CatalogViewedProductCallback";
     }
     if (strlen($arFields["MODULE"]) <= 0) {
         $arFields["MODULE"] = "catalog";
     }
     if (strlen($arFields["PRODUCT_PROVIDER_CLASS"]) <= 0 && $arFields["MODULE"] == "catalog") {
         $arFields["PRODUCT_PROVIDER_CLASS"] = "CCatalogProductProvider";
     }
     if ($arFields["PRODUCT_ID"] <= 0) {
         return false;
     }
     if (strlen($arFields["LID"]) <= 0) {
         return false;
     }
     if (\Bitrix\Main\Loader::includeModule("catalog")) {
         if (\Bitrix\Main\Config\Option::get("sale", "viewed_capability", "") == "Y") {
             return \Bitrix\Catalog\CatalogViewedProductTable::refresh($arFields["PRODUCT_ID"], CSaleBasket::GetBasketUserID(), $arFields["LID"]);
         }
     }
     $arFilter = array();
     $arFilter["PRODUCT_ID"] = $arFields["PRODUCT_ID"];
     if ($arFields["USER_ID"] > 0) {
         $arFuserItems = CSaleUser::GetList(array("USER_ID" => $arFields["USER_ID"]));
         $FUSER_ID = $arFuserItems["ID"];
     } elseif (IntVal($arFields["FUSER_ID"]) > 0) {
         $FUSER_ID = $arFields["FUSER_ID"];
     } else {
         $FUSER_ID = CSaleBasket::GetBasketUserID();
     }
     $arFilter["FUSER_ID"] = $FUSER_ID;
     $arFields["FUSER_ID"] = $FUSER_ID;
     $db_res = CSaleViewedProduct::GetList(array(), $arFilter, false, false, array('ID'));
     if (!($arItems = $db_res->Fetch())) {
         if (CModule::IncludeModule('catalog')) {
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider($arFields)) {
                 $arResultTmp = $productProvider::ViewProduct(array("PRODUCT_ID" => $arFields["PRODUCT_ID"], "USER_ID" => $arFields["USER_ID"], "SITE_ID" => $arFields["LID"]));
             } else {
                 $arResultTmp = CSaleBasket::ExecuteCallbackFunction($arFields["CALLBACK_FUNC"], $arFields["MODULE"], $arFields["PRODUCT_ID"], $arFields["USER_ID"], $arFields["LID"]);
             }
             if ($arResultTmp && count($arResultTmp) > 0) {
                 $arFields = array_merge($arFields, $arResultTmp);
             }
             if (strlen($arFields["NAME"]) <= 0) {
                 return false;
             }
             $arInsert = $DB->PrepareInsert("b_sale_viewed_product", $arFields);
             //chance deleted
             $rnd = mt_rand(0, 1000);
             if ($rnd < 100) {
                 $db_res = CSaleViewedProduct::GetList(array(), array("FUSER_ID" => $FUSER_ID), array("COUNT" => "ID"), false);
                 $arCount = $db_res->Fetch();
                 $viewedCount = COption::GetOptionString("sale", "viewed_count", "100");
                 if ($arCount["ID"] > IntVal($viewedCount)) {
                     $limit = $arCount["ID"] - $viewedCount + $viewedCount * 0.2;
                     CSaleViewedProduct::DeleteForUser($FUSER_ID, $limit);
                 }
             }
             $strSql = "INSERT INTO b_sale_viewed_product (" . $arInsert[0] . ", DATE_VISIT) VALUES(" . $arInsert[1] . ", " . $DB->GetNowFunction() . ")";
             $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             $ID = IntVal($DB->LastID());
         }
     } else {
         $ID = IntVal($arItems["ID"]);
         $arFields["DATE_VISIT"] = $DB->GetNowFunction();
         $arInsert = $DB->PrepareInsert("b_sale_viewed_product", $arFields);
         CSaleViewedProduct::Update($ID, $arFields);
     }
     foreach (GetModuleEvents("sale", "OnViewedAdd", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($arFields));
     }
     return $ID;
 }
<?php

require $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php";
if (CModule::IncludeModule("sale")) {
    CSaleViewedProduct::DeleteForUser(CSaleBasket::GetBasketUserID(), $LIMIT = NULL);
}