I
I
IliaMal2021-06-06 10:51:10
1C-Bitrix
IliaMal, 2021-06-06 10:51:10

Why is the price (API) not transmitted when placing an order in 1 click?

Hello.

If you place a "Quick order" for the current user, receiving a basket like this:

$basket = Sale\Basket::loadItemsForFUser(Sale\Fuser::getId(), Bitrix\Main\Context::getCurrent()->getSite());

then everything is correct: 60bc7bb9e1e0a931464643.png
If I want to buy one product by passing its ID, like this:
$ID = 	520747;
$price = CPrice::GetBasePrice($ID)['PRICE'];
$products = array(
    array(
        'PRODUCT_ID' => $ID,
        'NAME' => "Бс-100/24",
        'PRICE' => $price,
        'CURRENCY' => 'RUB',
        'QUANTITY' => 1
    )
);
$basket = Bitrix\Sale\Basket::create(SITE_ID);
foreach ($products as $product)
{
    $item = $basket->createItem("dress_catalog", $product["PRODUCT_ID"]);
    unset($product["PRODUCT_ID"]);
    $item->setFields($product);
}

then the order is created, but the desired product is not attached.
60bc7d781ade7979279538.png

In all cases, this option is duplicated, I did not find another way to add a product to the order.
Here is the whole code .

Where is the mistake?
Is there another way to place an order?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ex1st, 2021-06-07
@IliaMal

Try my code:

$basket = \Bitrix\Sale\Basket::create('s3');
$basket = $basket->getOrderableItems();

foreach ($basket as $item) {
    $item->getItemById($id)->delete();
}

$item = $basket->createItem("catalog", $id);
$item->setFields(array(
    'PRODUCT_ID' => $id,
    'CURRENCY' => 'RUB',
    'QUANTITY' => $quantity,
    'LID' => SITE_ID,
    'PRODUCT_PROVIDER_CLASS' => '\CCatalogProductProvider'
));

// order
$order = \Bitrix\Sale\Order::create(SITE_ID, $USER->isAuthorized() ? $USER->GetID() : $ID);
$order->setPersonTypeId(5);
$order->setBasket($basket);

//shippment collection
$shipColl = $order->getShipmentCollection();
$shipp = $shipColl->createItem(\Bitrix\Sale\Delivery\Services\Manager::getObjectById(1));

$shipColl = $shipp->getShipmentItemCollection();

foreach ($basket as $basketItem) {
    $item = $shipColl->createItem($basketItem);
    $item->setQuantity($basketItem->getQuantity());
}

//props
$propsColl = $order->getPropertyCollection();

$order->setField('COMMENTS', 'Это быстрый заказ. Нужно перезвонить клиенту.');

$phoneProp = $propsColl->getPhone();
$phoneProp->setValue($phone);

$nameProp = $propsColl->getProfileName();
$nameProp->setValue($name);

$emailProp = $propsColl->getUserEmail();
$emailProp->setValue($email);

// pay
$payColl = $order->getPaymentCollection();
$payment = $payColl->createItem(\Bitrix\Sale\PaySystem\Manager::getObjectById(13));

$payment->setField("SUM", $order->getPrice());
$payment->setField("CURRENCY", $order->getCurrency());

//save
$order->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question