V
V
Vladimir2018-11-22 16:21:02
1C-Bitrix
Vladimir, 2018-11-22 16:21:02

How to fix the problem - updating the cart data resets the price of the product to the minimum?

<b>$basket->refreshData(['PRICE']);</b>
            
            $discounts = Sale\Discount::buildFromBasket($basket, new Sale\Discount\Context\Fuser($fuserId));
            $discounts->calculate();
            $showPrices = $discounts->getShowPrices();
            
            $arItems = [];
            $obBasket = Sale\Basket::getList([
                'filter' => [
                    'FUSER_ID' => $fuserId, 
                    "ORDER_ID" => null, 
                    'LID' => \Bitrix\Main\Context::getCurrent()->getSite(), 
                    'CAN_BUY' => 'Y',
                    'DELAY' => 'N'
                ],
                'select' => [
                    "ID", "NAME", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY",
                    "CURRENCY", "SUBSCRIBE", "PRODUCT_PROVIDER_CLASS", "PRODUCT_PRICE_ID"
                ]
            ]);
            while($bItem = $obBasket->Fetch()){
                $bItem['PRICE'] = Sale\PriceMaths::roundPrecision($showPrices['BASKET'][$bItem['ID']]['SHOW_PRICE']);
                $bItem['BASE_PRICE'] = Sale\PriceMaths::roundPrecision($showPrices['BASKET'][$bItem['ID']]['SHOW_BASE_PRICE']);
                $bItem['DISCOUNT_PRICE'] = Sale\PriceMaths::roundPrecision($showPrices['BASKET'][$bItem['ID']]['SHOW_DISCOUNT']);
                
                $bItem['SUM'] = $bItem['PRICE'] * (float)$bItem['QUANTITY'];
                
                $bItem['PRINT_SUM'] = \CCurrencyLang::CurrencyFormat($bItem['SUM'], $bItem['CURRENCY'], true);
                $bItem['PRINT_BASE_PRICE'] = \CCurrencyLang::CurrencyFormat($bItem['BASE_PRICE'], $bItem['CURRENCY'], true);
                $bItem['PRINT_PRICE'] = \CCurrencyLang::CurrencyFormat($bItem['PRICE'], $bItem['CURRENCY'], true);

                $arItems[$bItem['ID']] = $bItem;
            }

The function calculates discounts for the product added to the cart. But when doing $basket->refreshData(['PRICE']); the previously saved price for the product is reset to the minimum of the price type and discounts are already calculated for this price. How to fix this problem?
When I add a product to the cart, I strictly specify which price type to use (see below).
Function of adding goods to cart:
$item = $basket->createItem('catalog', $productId);
                    
                $propertyCollection = $item->getPropertyCollection();
                if ($propertyCollection)
                    $propertyCollection->setProperty($arProps);

                $PRICE = \CPrice::GetList(false, ['PRODUCT_ID' => $productId, 'CATALOG_GROUP_NAME' => $PRICE_CODE])->fetch();
                $item->setFields([
                    'QUANTITY' => $quantity,
                    'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
                    'LID' => \Bitrix\Main\Context::getCurrent()->getSite(),
                    'PRODUCT_PROVIDER_CLASS' => \Bitrix\Catalog\Product\Basket::getDefaultProviderName(),
                    'PRICE' => $PRICE['PRICE'],
                    "BASE_PRICE" => $PRICE['PRICE'],
                    "PRICE_TYPE_ID" => $PRICE["CATALOG_GROUP_ID"],
                    "PRODUCT_PRICE_ID" => $PRICE["ID"],
                ]);
                
                $item->save();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2018-11-22
@winer

this is normal behavior. Each time you visit the shopping cart page, the product prices are updated. In this case, the price of the goods is taken from the minimum available.
Availability of the price type for user groups is configured on the price page in the admin panel.
To change this behavior, you need to change PRODUCT_PROVIDER_CLASS to your own. Can it be like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question