A
A
Alexander2021-09-28 11:06:07
1C-Bitrix
Alexander, 2021-09-28 11:06:07

Why is the price not added at checkout?

There is an example of placing an order via API on the Bitrix

website. I apply it by substituting my data, everything is recorded except for the price, it goes 0.
$order->getPrice()Does not find the right price. From the form, when I insert $request['ELEMENT_PRICE']it, it also does not write, (it writes in the comments, but not in the order). There are no errors in the console. In the admin panel, too, everything is 0

<? require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");

use Bitrix\Main\Application;

$application = Application::getInstance();
$context = $application->getContext();
global $APPLICATION;
global $USER;

$request = \Bitrix\Main\HttpApplication::getInstance()->getContext()->getRequest();

\Bitrix\Main\Web\Json::encode($request);

if ($request->isPost() && isset($request['ELEMENT_ID']) && isset($request['PHONE']) && isset($request['EMAIL']) && isset($request['ELEMENT_PRICE'])) {


    Bitrix\Main\Loader::includeModule('sale');
    Bitrix\Main\Loader::includeModule('catalog');

    /*$products = array(
        array('PRODUCT_ID' => $request['ELEMENT_ID'], 'NAME' => $request['ELEMENT_NAME'], 'PRICE' => $request['ELEMENT_PRICE'], 'CURRENCY' => 'RUB', 'QUANTITY' => 1)
    );*/

    $products = array(
        array('PRODUCT_ID' => 699, 'NAME' => 'Товар 1', 'PRICE' => 800, 'CURRENCY' => 'RUB', 'QUANTITY' => 5)
    );

    $basket = Bitrix\Sale\Basket::create(SITE_ID);

    foreach ($products as $product) {
        $item = $basket->createItem("catalog", $product["PRODUCT_ID"]);
        unset($product["PRODUCT_ID"]);
        $item->setFields($product);
    }

    $order = Bitrix\Sale\Order::create(SITE_ID, 1);
    $order->setPersonTypeId(1);
    $order->setBasket($basket);

    $shipmentCollection = $order->getShipmentCollection();
    $shipment = $shipmentCollection->createItem(
        Bitrix\Sale\Delivery\Services\Manager::getObjectById(1)
    );

    $shipmentItemCollection = $shipment->getShipmentItemCollection();

    /** @var Sale\BasketItem $basketItem */

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

    $paymentCollection = $order->getPaymentCollection();
    $payment = $paymentCollection->createItem(
        Bitrix\Sale\PaySystem\Manager::getObjectById(2)
    );
    $payment->setField("SUM", $order->getPrice());
    $payment->setField("CURRENCY", $order->getCurrency());

    $order->doFinalAction(true);

    // Устанавливаем свойства
    $propertyCollection = $order->getPropertyCollection();
    foreach ($propertyCollection->getGroups() as $group) {
        foreach ($propertyCollection->getGroupProperties($group['ID']) as $property) {

            $p = $property->getProperty();
            if ($p["CODE"] == "EMAIL") {
                $property->setValue($request['EMAIL']);
            }
            if ($p["CODE"] == "PHONE") {
                $property->setValue($request['PHONE']);
            }
            if ($p["CODE"] == "NAME") {
                $property->setValue($request['NAME']);
            }

        }
    }

    $order->setField('COMMENTS', $request['ELEMENT_PRICE'] . " get price =" . $order->getPrice());

    $result = $order->save();
    if (!$result->isSuccess()) {
        echo $result->getErrors();
    } else {
        \Bitrix\Main\Web\Json::encode("ok");
    }
} else {
    echo "ERROR <br>";
    echo "ELEMENT_ID " . $request['ELEMENT_ID'] . " НАзвание " . $request['ELEMENT_NAME'] . " PHONE " . $request['PHONE'] . " EMAIL " . $request['EMAIL'] . " ELEMENT_PRICE " . $request['ELEMENT_PRICE'];
} ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Novozhilov, 2021-09-28
@kikher

So,
if you need a custom product price: the product
array should look like

array(
    'PRODUCT_ID' => 699, 
    'NAME' => 'Товар 1', 
    'PRICE' => 800, 
    'CURRENCY' => 'RUB', 
    'QUANTITY' => 5
    'CUSTOM_PRICE' => 'Y'
)

if you need to pull from a directory:
array(
    'PRODUCT_ID' => 699, 
    'NAME' => 'Товар 1', 
    'CURRENCY' => 'RUB', 
    'QUANTITY' => 5,
    'PRODUCT_PROVIDER_CLASS' => '\CCatalogProductProvider'
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question