L
L
Leonid2020-05-03 19:31:50
1C-Bitrix
Leonid, 2020-05-03 19:31:50

How to fill in the weight and dimensions of goods in Bitrix?

Good afternoon! Faced the problem of filling in the weight and dimensions of the goods in Bitrix.
Unloading from 1C is configured, after which the weight and size data are filled in the details, and a handler for the OnAfterIBlockElementAdd and OnAfterIBlockElementUpdate events is also configured in init.php.
An array of data is formed in the handler:
Array
(
[LENGTH] => 23
[WIDTH] => 21
[HEIGHT] => 42
)
And using the CCatalogProduct::Update($PRODUCT_ID, $arFields); I load the data into the required fields.
But nothing happens. I shoveled a bunch of sites, where it is advised to do it through the Add method (or add it before the update method), I also tried, but no, somewhere it says that the element is not a product and I need to resave it, it didn’t work out for me.

I tried before and after the update method to display the product properties using the GetByID method. Before the method, these three properties are empty, but after they are filled with the necessary fields. It would seem that everything worked out, but when you enter the product, the properties are empty. I don't know what to do or where to look. The documentation says that these methods are deprecated, but they seem to be supported. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jupiter Max, 2020-05-03
@vardoLP

Something is somehow incomprehensible! You load properties from 1s. Why do you need init.php?

I
Ilya, 2020-05-04
@rpsv

The weight must be processed, check the exchange settings from the 1C side (you can see the upload file itself, there should be a weight tag in the offer or product (not a prop, but a tag). If not, then check the 1C settings.
If dancing with a tambourine towards 1C if it didn’t help, then modify it from the side of the site. To begin with, it’s not worth hanging on the event of changing/adding an element, because if there are a lot of goods, and exchanges are frequent, then the exchange time will increase extremely. updating weights for all products.

Handler

AddEventHandler("catalog", "OnBeforeCatalogImport1C", "CatalogWeightProcessor::OnBeforeCatalogImport1C");
AddEventHandler("catalog", "OnSuccessCatalogImport1C", "CatalogWeightProcessor::OnSuccessCatalogImport1C");

class CatalogWeightProcessor
{
    protected static $exchangeStart = null;

    public static function OnBeforeCatalogImport1C()
    {
        // запоминаем время начала обмена, чтобы вытащить только обновленные товары
        self::$exchangeStart = time();
    }

    public static function OnSuccessCatalogImport1C()
    {
        Bitrix\Main\Loader::includeModule('iblock');

        $select = [
            'ID',
            'PROPERTY_[вес]',
        ];
        $filter = [
            '=IBLOCK_ID' => "ид каталога",
            '>=TIMESTAMP_X' => Bitrix\Main\Type\DateTime::createFromTimestamp(self::$exchangeStart),
        ];
        $result = CIBlockElement::GetList([], $filter, false, false, $select);
        while ($row = $result->GetNext()) {
            $productId = $row['ID'];
            $weight = (float) $row['PROPERTY_[вес]_VALUE'];
            $result = Bitrix\Catalog\ProductTable::update($productId, [
                'WEIGHT' => $weight,
            ]);
            if (!$result->isSuccess()) {
                $errors = $result->getErrorMessages();
                CEventLog::Add([
                    'SEVERITY' => 'ERROR',
                    'AUDIT_TYPE_ID' => 'exchange1c',
                    'MODULE_ID' => 'catalog',
                    'ITEM_ID' => $productId,
                    'DESCRIPTION' => join(",", $errors),
                ]);
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question