Answer the question
In order to leave comments, you need to log in
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);
[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')
Answer the question
In order to leave comments, you need to log in
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question