Order property values
Attention! Saves must be performed via the order: \Bitrix\Sale\Order::save()
. Additionally, when calling \Bitrix\Sale\PropertyValueCollection::save()
or \Bitrix\Sale\PropertyValue::save()
will generate error level E_WARNING
.
Getting property values:
- Handling objects
$collection = $order->getPropertyCollection(); foreach ($collection as $item) { // $item - object of class \Bitrix\Sale\PropertyValue }
- Handling arrays
In addition to objects, there is an option to directly query the database via the method
\Bitrix\Sale\PropertyValueCollection::getList($parameters)
, or\Bitrix\Sale\PropertyValue::getList($parameters)
. Both these methods work similarly.Methods always return object
Bitrix\Main\DB\Result
, that can be used to retrieve data using the methodfetch()
.Parameter Description Available from version $parameters Array with structure fully matches the structure ORM's getList. // getting property list for order with ID 123 $dbRes = \Bitrix\Sale\PropertyValueCollection::getList([ 'select' => ['*'], 'filter' => [ '=ORDER_ID' => 123, ] ]); while ($item = $dbRes->fetch()) { var_dump($item); }
Getting specific property value
$collection = $order->getPropertyCollection();
- by ID
$propertyValue = $collection->getItemById($id);
- by internal index
$propertyValue = $collection->getItemByIndex($index);
- by property ID
$propertyValue = $collection->getItemByOrderPropertyId($propertyId);
- getters for key property values (phone, email, name, address and etc.)
$propertyValue = $collection->getAddress(); $propertyValue = $collection->getPhone(); $propertyValue = $collection->getProfileName(); $propertyValue = $collection->getTaxLocation(); $propertyValue = $collection->getDeliveryLocationZip(); $propertyValue = $collection->getDeliveryLocation(); $propertyValue = $collection->getPayerName(); $propertyValue = $collection->getUserEmail();
Adding new property
- Variant 1:
$propertyValue = $collection->createItem([ 'ID' => 1, 'NAME' => 'New property', 'TYPE' => 'STRING', 'CODE' => 'NEW_PROP', ]); $propertyValue->setField('VALUE', 'Y');
- Variant 2:
$propertyValue = \Bitrix\Sale\PropertyValue::create($collection, [ 'ID' => 1, 'NAME' => 'New property', 'TYPE' => 'STRING', 'CODE' => 'NEW_PROP', ]); $propertyValue->setField('VALUE', 'Property_3'); $collection->addItem($propertyValue);
Updating property value
$order = \Bitrix\Sale\Order::load(123); $collection = $order->getPropertyCollection(); $propertyValue = $collection->getItemById(123); $r = $propertyValue->setField('VALUE', 'New value'); if (!$r->isSuccess()) { var_dump($r->getErrorMessages()); } $order->save();
Field, available for editing:
"VALUE", // property value
Deleting property value
$order = \Bitrix\Sale\Order::load(123); $collection = $order->getPropertyCollection(); $propertyValue = $collection->getItemById(123); $propertyValue->delete(); $order->save();
Getting property data with value stored in order:
- Handling an array
$propertyInfo = $propertyValue->getProperty();
Result for array with keys:
'ID', // property ID 'PERSON_TYPE_ID', // payer type ID with property 'NAME', // Property name 'TYPE', // Type 'REQUIRED', // Flag sets parameter as required 'DEFAULT_VALUE', // Default value 'SORT', // Sorting 'USER_PROPS', // Included in profile 'IS_LOCATION', // Use as location 'PROPS_GROUP_ID', // group ID 'DESCRIPTION', // Description 'IS_EMAIL', // Use as e-mail 'IS_PROFILE_NAME', // Use as profile name 'IS_PAYER', // Use as payer name 'IS_LOCATION4TAX', // Use as tax location 'IS_FILTERED', // Property is available in orders filter 'CODE', // Code 'IS_ZIP', // Use as zip code 'IS_PHONE', // Use as phone 'IS_ADDRESS', // Use as address 'ACTIVE', // Flag sets as active 'UTIL', // Flag sets as service parameter 'INPUT_FIELD_LOCATION', 'MULTIPLE',// Flag sets as multiple 'MINLENGTH', // Minimum string length 'MAXLENGTH', // Maximum string length 'PATTERN', // Validation regular expression 'MULTILINE', // Multiple strings 'SIZE', // Number of entered characters
- Handling an object
$property = $propertyValue->getPropertyObject();
Any value can be retrieved using the method:
$property->getField('SIZE');
Some more popular fields have getters:
$property->getId(); // property ID $property->getPersonTypeId(); // payer type ID with property $property->isUtil(); // use as service parameter $property->isRequired(); // use as required parameter $property->getType(); // type $property->getDescription(); // description $property->getRelations(); // relations $property->getName(); // name $property->getGroupId(); // group ID
© «Bitrix24», 2001-2024