Your IP : 3.145.1.125


Current Path : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/OnProductAddUpdate.php

<?php

namespace Epages;

use CModule;
use CIBlockElement;

\CModule::IncludeModule('catalog');
\CModule::IncludeModule('sale');
require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");

class OnProductAddUpdate
{
    public static function setMinPrice($ID, $arFields)
    {


        $cntProduct = 0;
        $cntWBPProduct = 0;

        $arProductID = $ID;
        $minPrice = false;
        $minDiscount = false;

        /**
         * get discount of product
         */
        $dbProductDiscounts = \CCatalogDiscount::GetList(
            false,
            array(
                "+PRODUCT_ID" => $arProductID,
                "ACTIVE" => "Y",
            ),
            false,
            false,
            array(
                "VALUE",
            )
        );
        while ($arProductDiscounts = $dbProductDiscounts->Fetch()) {
            $DISCOUNT = $arProductDiscounts['VALUE'];
            if ($minDiscount === false || $minDiscount > $DISCOUNT) {
                $minDiscount = $DISCOUNT;
            }
        }

        /**
         * get prices
         */
        $rsPrices = \CPrice::GetList(
            array(),
            array(
                "PRODUCT_ID" => $arProductID,
            )
        );
        $basePrice = \CPrice::GetBasePrice(
            $arProductID
        );

        while ($arPrice = $rsPrices->Fetch()) {
            $PRICE = [$arPrice["PRICE"], ($minDiscount != false) ? $minDiscount : $arPrice["PRICE"]];
            foreach ($PRICE as $keyAllPrice => $valueAllPrice) {
                if ($minPrice === false || $minPrice > $valueAllPrice) {
                    $minPrice = $valueAllPrice;
                }
            }
        }

        /**
         * save found minimal price into property
         */
        if ($minPrice !== false && $basePrice !== false) {
            \CIBlockElement::SetPropertyValuesEx(
                $arProductID,
                false, //$IBLOCK_ID,
                array(
                    "MINIMUM_PRICE" => $minPrice,
                )
            );
        } else {
            \CIBlockElement::SetPropertyValuesEx(
                $arProductID,
                false, //$IBLOCK_ID,
                array(
                    "MINIMUM_PRICE" => "",
                )
            );
        }
    }
}