P
P
pantaleone482021-03-11 11:38:55
1C-Bitrix
pantaleone48, 2021-03-11 11:38:55

1C Bitrix. Why does the price change by event not work?

It is necessary to change the base price, depending on other types of prices. I'm trying to implement a price update through the OnBeforePriceUpdate event. If you save the product through the form, then it is updated after the second save, while unloading the base price does not change.

AddEventHandler("catalog", "OnBeforePriceUpdate", Array("UpdateProduct", "newPrice"));

class UpdateProduct{

    protected static $handlerDisallow = 0;

    public static function disableHandler()
    {
        self::$handlerDisallow--;
    }

    public static function enableHandler()
    {
        self::$handlerDisallow++;
    }

    public static function isEnabledHandler()
    {
        return (self::$handlerDisallow >= 0);
    }

    public static function newPrice($ID, &$arFields){
        /* проверяем, что обработчик уже запущен */
        if (!self::isEnabledHandler())
            return;
        /* взводим флаг запуска */
        self::disableHandler();

        \Bitrix\Main\Loader::includeModule("catalog");

        $allProductPrices = \Bitrix\Catalog\PriceTable::getList([
            "select" => [
                "ID",
                "PRICE",
                "CATALOG_GROUP_ID"
            ],
            "filter" => [
                "PRODUCT_ID" => $arFields["PRODUCT_ID"],
            ]
        ])->fetchAll();

        $addProps = CIBlockElement::GetList(
            Array(),
            Array("ID" => $arFields["PRODUCT_ID"]),
            false,
            false,
            Array(
                'ID', "NAME", "PROPERTY_NO_UPDATE_PRICE", "PURCHASING_PRICE"
            )
        );
        if($ar_res = $addProps->GetNext()){
            foreach($allProductPrices as $key => $value){
                if($allProductPrices[$key]["CATALOG_GROUP_ID"] == 1){
                    $arPrice["BASE_ID"] = $allProductPrices[$key]["ID"];
                    $arPrice["BASE_PRICE"] = $allProductPrices[$key]["PRICE"];
                } elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 2){
                    $arPrice["MERLION_PRICE"] = $allProductPrices[$key]["PRICE"];
                } elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 3){
                    $arPrice["MONITORING_PRICE"] = $allProductPrices[$key]["PRICE"];
                } elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 4){
                    $arPrice["TEHNO_PRICE"] = $allProductPrices[$key]["PRICE"];
                }
            }

            if($ar_res["PROPERTY_NO_UPDATE_PRICE_VALUE"] == "Y"){
                if(isset($arPrice["TEHNO_PRICE"]) and $arPrice["TEHNO_PRICE"] != 0){
                    $newPrice["PRICE"] = $arPrice["TEHNO_PRICE"];
                }
            } else{
                if(isset($arPrice["MONITORING_PRICE"]) and $arPrice["MONITORING_PRICE"] != 0){
                    if(((($arPrice["MONITORING_PRICE"] - $ar_res["PURCHASING_PRICE"]) * 100) / $arPrice["MONITORING_PRICE"]) <= -5){
                        $newPrice["PRICE"] = 0;
                        $update = new CIBlockElement();
                        $res2 = $update->Update($ar_res["ID"], array("ACTIVE" => "N"));
                        if ($res2->isSuccess()){
                            AddMessage2Log(date("d.m.Y H:i:s") . " | Закупочная цена меньше розничной на 5 и более процентов - ".$arFields["PRODUCT_ID"], "");
                        }
                    } else{
                        $newPrice["PRICE"] = $arPrice["MONITORING_PRICE"];
                    }
                } elseif(isset($arPrice["TEHNO_PRICE"]) and $arPrice["TEHNO_PRICE"] != 0){
                    $newPrice["PRICE"] = $arPrice["TEHNO_PRICE"];
                } elseif(isset($arPrice["MERLION_PRICE"]) and $arPrice["MERLION_PRICE"] != 0){
                    $newPrice["PRICE"] = $arPrice["MERLION_PRICE"];
                }
            }
            if($arPrice["BASE_PRICE"] != $newPrice["PRICE"] and $newPrice["PRICE"] > 0){
                $arFields["PRICE"] = $newPrice["PRICE"];
                AddMessage2Log(date("d.m.Y H:i:s") . " | Изменение цены на ".$arFields["PRICE"]." для ".$ar_res["NAME"]." ".$arFields["PRODUCT_ID"], "");

            }

            /* вновь разрешаем запускать обработчик */
            self::enableHandler();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Olga Veter, 2018-01-09
@vetero4eg

Take a look at examples and documentation kenwheeler.github.io/slick
You need parameters slidesToShow: __ , slidesToScroll: __

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question