I
I
IliaMal2021-03-03 09:01:10
1C-Bitrix
IliaMal, 2021-03-03 09:01:10

Why does an error occur when filling in the order field when creating an order via Bitrix\Sale\Order?

Hello.

To create an order in one click, I used the Bitrix\Sale\Order class.
There was a problem with filling in the properties of the order, full name, phone, mail of the buyer.
I did not find information in the documentation on how to fill in these particular fields, I just found that they can be filled through the getPropertyCollection () method.
At random, it was possible to fill them like this:

$collection = $order->getPropertyCollection();

$propertyValue = $collection->createItem([
    'ID' => 1,
    'NAME' => 'NAME',
    'TYPE' => 'STRING',
    'CODE' => 'PROP_Name',
]);
$propertyValue->setField('VALUE', $name);
$propertyValue = $collection->createItem([
    'ID' => 2,
    'NAME' => 'PHONE',
    'TYPE' => 'STRING',
    'CODE' => 'PROP_Phone',
]);
$propertyValue->setField('VALUE', $phone);
$propertyValue = $collection->createItem([
    'ID' => 3,
    'NAME' => 'EMAIL',
    'TYPE' => 'STRING',
    'CODE' => 'PROP_Email',
]);
$propertyValue->setField('VALUE', $email);


Orders were created correctly until Bitrix was updated.
Now gives mysql error
[Bitrix\Main\DB\SqlQueryException] 
Mysql query error: (1062) Duplicate entry '1020-1' for key 'IX_SOPV_ORD_PROP_UNI' (400)
INSERT INTO `b_sale_order_props_value`(`ORDER_ID`, `ORDER_PROPS_ID`, `NAME`, `VALUE`, `CODE`, `XML_ID`) VALUES (1020, 1, 'NAME', '[email protected]', 'PROP_Name', 'bx_603f258e31ae7')


An order is created, but information about the buyer is not saved in the order properties.

Please tell me how to pass these properties.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2021-03-04
@IliaMal

Some of the properties are duplicated. Most likely an email.
Try to get these properties from $propertyCollection first, and if they are not there, then create them already.
Example for email

if ($emailProp = $collection->getUserEmail()) {
  $emailProp->setValue($email);
} else {
  $emailProp = $collection->createItem([
    'ID' => 3,
    'NAME' => 'NAME',
    'TYPE' => 'STRING',
    'CODE' => 'PROP_Email',
  ]);
  $emailProp->setField('VALUE', $email);
}

This code will work if the "Property value will be used as E-Mail when registering a new user" checkbox is checked in the email property settings. If it is not worth it, then it will be necessary to bypass the propertyCollection in the loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question