U
U
userberg2020-07-03 15:34:40
1C-Bitrix
userberg, 2020-07-03 15:34:40

Bitrix Reset to zero when the property is active. as?

Tell me where to dig, full 0.
earlier, when setting the property (withdrawn from sale), the rest of the goods were reset, the available quantity field.
The product remained hanging on the site, but it was impossible to order.
After the next update (unfortunately I have no idea which one), this processing stopped working.
In the init.php file, I found a function that was responsible for this.

AddEventHandler("catalog", "OnBeforeProductAdd", "OnBeforeProductAddUpdateHandler");
AddEventHandler("catalog", "OnBeforeProductUpdate", "OnBeforeProductAddUpdateHandler");

function OnBeforeProductAddUpdateHandler(&$arFields)
{
    $ID = $arFields['ID'];

    //Обнуляем остатки и запрещаем продавать в минус при активном свойстве "Снят с продажи"
    if (CModule::IncludeModule("iblock")) {

        $rsElement = CIBlockElement::GetByID($ID);
        if ($obElement = $rsElement->GetNextElement()) {

            $arElement = $obElement->GetFields();
            $arElement["PROPERTIES"] = $obElement->GetProperties();

            if (intval($arElement["IBLOCK_ID"]) === 30) {

                if (!empty($arElement["PROPERTIES"]["NOT_FOR_SALE"]["VALUE"])) {
                    $arFields["QUANTITY"] == 0;
                    $arFields["CAN_BUY_ZERO"] == "N";
                } else {
                    $arFields["CAN_BUY_ZERO"] == "D";
                }
            }
        }
    }
}


it is necessary to reset the residuals, just displaying the button with the active property is not suitable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2020-07-03
@anton99zel

Cmodule::IncludeModule('catalog');
 $el = new CIBlockElement;
if (!empty($arElement["PROPERTIES"]["NOT_FOR_SALE"]["VALUE"])) {
            $arFields = array("QUANTITY" => 0, "CAN_BUY_ZERO"=> "N");
            CCatalogProduct::Update($ID, $arFields);
                } else {
            $arFields = array("CAN_BUY_ZERO"=> "D");
            CCatalogProduct::Update($ID, $arFields);
                }

You also need to pay attention to NOT_FOR_SALE - check it for emptiness or for a value, because a property can be a checkbox, a number, a string .... how are you?
And, perhaps, in the settings of the module "Trade catalog" you need to enable support for obsolete events

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question