Y
Y
Yuri Voronin2019-03-29 14:56:29
1C-Bitrix
Yuri Voronin, 2019-03-29 14:56:29

How to add a product to cart via API?

Hello. I'm adding items to the cart. I use CSaleBasket::Add($arFields); . The product is added, but how to add the price of the product correctly? I do like this:

CModule::IncludeModule("sale");
                CModule::IncludeModule("product");
                CModule::IncludeModule("iblock");
                $arFilterOffers = array("IBLOCK_TYPE" => "catalog", 'ID' => $id);
                $rsOffers = CIBlockElement::GetList(array(),$arFilterOffers, false, false, array("NAME", "CATALOG_GROUP_2", "XML_ID", "EXTERNAL_ID", "DETAIL_PAGE_URL"));
                while($Offers = $rsOffers->GetNext()){
                    $arOffers = $Offers;
                }
                $dbPrices = CPrice::GetList(
                    array(),
                    array("PRODUCT_ID" => $id,"CATALOG_GROUP_ID" => 2),
                    false,
                    false,
                    array()
                );
                while($price = $dbPrices->Fetch()){
                    $OffersCost = $price;
                }

                $arFields = array(
                    "PRODUCT_ID" => $arOffers["ID"],
                    "PRODUCT_PRICE_ID" => $OffersCost["ID"],
                    "PRICE" => $OffersCost["PRICE"],
                    "CURRENCY" => "RUB",
                    "QUANTITY" => 1,
                    "LID" => 's1',
                    "DELAY" => "N",
                    "CAN_BUY" => "Y",
                    "NAME" => $arOffers["NAME"],
                    "MODULE" => "catalog",
                    "DETAIL_PAGE_URL" => $arOffers["DETAIL_PAGE_URL"]
                );
                CSaleBasket::Add($arFields);

In the field $arFields["PRICE"] I can insert any price. I can define a discounted price and add it. But after adding the product to the cart, the price will be the one that I indicated, but there will be no discount on the product. How to add a discount when adding a product to the cart? The discount was created in Bitrix.
Item in the cart, added via API (I send the price exactly 36900)
5c9e06d5ac087930389681.pngItem in the cart, added through the standard functionality from the catalog.
5c9e06df78b62334395584.png
Z.Y. In the CSaleBasket::Add($arFields); there is a parameter PRODUCT_PROVIDER_CLASS , I suppose you need to use it somehow. But I don't understand how.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2019-03-29
@anton99zel

You need to use the CSaleOrder::DoCalculateOrder method - this method will recalculate taking into account the discount at the moment the order is created.
But this is probably not needed here:

CModule::IncludeModule("product");
CModule::IncludeModule("iblock");

S
Sergey Panteleev, 2019-03-29
@s_panteleev

Try
And also look towards D7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question