M
M
monyavitevna2017-08-23 15:52:22
linux
monyavitevna, 2017-08-23 15:52:22

There is a php script, you need to write it to cron. how to do?

Hello!
There is a php script, you need to write it to cron. I try to start using the command line, an error. Code and error below:

<?php
if(CModule::IncludeModule("iblock") && CModule::IncludeModule("catalog")) 
{
   echo "Скрипт запущен...<br />";

   $newExtraId = 6; // ID нашей наценки
   $newPriceId = 2; // ID типа цены, для которого задаём эту наценку
   $catalogIblockType = "offers"; // код типа инфоблока, для которого проводим обработку

   $count = 0;
   $rsProducts = CCatalogProduct::GetList(
      array("ID" => "ASC"),
      array("IBLOCK_TYPE" => $catalogIblockType),
      false,
      false
   );
   while (($arProduct = $rsProducts->Fetch())/* && $count < 10*/) {
      $arPriceFields = Array(
         "PRODUCT_ID" => $arProduct["ID"],
         "CATALOG_GROUP_ID" => $newPriceId,
         "EXTRA_ID" => $newExtraId,
         "CURRENCY" => "RUB",
      );
      $rsProductPrices = CPrice::GetList(
         array(),
         array(
            "PRODUCT_ID" => $arProduct["ID"],
            "CATALOG_GROUP_ID" => $newPriceId
         )
      );
      if ($arProductPrice = $rsProductPrices->Fetch()) {
         if (CPrice::Update($arProductPrice['ID'], $arPriceFields, true) !== false) {
            print "Обновлена цена ".$arProductPrice['ID']." для товара ".$arProduct["ID"]."<br />";
         }
         else {
            print "Ошибка при обновлении цены ".$arProductPrice['ID']." для товара ".$arProduct["ID"].": ";
            if ($ex = $APPLICATION->GetException())
               echo $ex->GetString();
            echo "<br />";
         }
      }
      else {
         $priceId = CPrice::Add($arPriceFields, true);
         if ($priceId !== false) {
            print "Добавлена новая цена для товара ".$arProduct["ID"]." ".$priceId."<br />";
         }
         else {
            print "Ошибка при добавлении цены для товара ".$arProduct["ID"].": ";
            if ($ex = $APPLICATION->GetException())
               echo $ex->GetString();
            echo "<br />";
         }
      }

      $count++;
   }
   echo "Скрипт завершён. Обработано ".$count." товаров.";
}
?>
Ошибка: "Uncaught Error: Class 'CModule' not found "

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Emelyanov, 2017-08-23
@monyavitevna

Add to the beginning:

$_SERVER["DOCUMENT_ROOT"] = '/home/bitrix/www';
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

A
Andrey Nikolaev, 2017-08-23
@gromdron

To get started, check out the available documentation: https://dev.1c-bitrix.ru/learning/course/?COURSE_I...
At the bottom of the page (link above) there is an agent code that runs under cron. He'll do.
The first thing to pay attention to is the SITE_ID constant. If you are supposed to work from the admin panel, then this is a language constant, otherwise it is a site constant (it is better to set it manually)
Second: check $_SERVER["DOCUMENT_ROOT"] (if you have bitrix vm, then this is most likely the same path)
And the third - instead of echo in cli mode it is better to use: fwrite( STDOUT, 'String' );

A
Anatoly Kirsanov, 2017-08-23
@bitrixworkshop

There is a great example in /bitrix/modules/main/tools/cron_events.php
Well, then PHP CLI.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question