Shopping cart properties
Attention!When shopping cart is associated with an order It's categorically prohibited to use the method \Bitrix\Sale\Basket::save()
for saving shipments. Saving action must be performed via the order method: [link=\Bitrix\Sale\OrderBase::save]\Bitrix\Sale\Order::save()
. Additionally, calling \Bitrix\Sale\BasketPropertiesCollection::save()
will generate error E_WARNING
.
Getting properties for product item in shopping cart
- Handling collection
$collection = $basketItem->getPropertyCollection(); foreach ($collection as $item) { // $item - объект класса \Bitrix\Sale\BasketPropertyItem }
-
Handling an array
In addition to objects, there is an option to directly query database using the method
\Bitrix\Sale\BasketPropertiesCollection::getList($parameters)
.Method always returns object
Bitrix\Main\DB\Result
and retrieve data using the method the methodfetch()
.Parameter Description Available from version $parameters Array with structure fully matching ORM's getList. // getting list of properties for product with ID 123 $dbRes = \Bitrix\Sale\BasketPropertiesCollection::getList([ 'select' => ['*'], 'filter' => [ '=BASKET_ID' => 123, ] ]); while ($item = $dbRes->fetch()) { var_dump($item); }
Getting a specific property
$collection = $baketItem->getPropertyCollection();
- by ID
$property = $collection->getItemById($id);
- by internal index
$property = $collection->getItemByIndex($index);
Adding a new property
// $basket - object of class \Bitrix\Sale\Basket foreach ($basket as $basketItem) { $collection = $basketItem->getPropertyCollection(); $item = $collection->createItem(); $item->setFields([ 'NAME' => 'New property', 'CODE' => 'XXX', 'VALUE' => 'Value', ]); }
Updating a property
// $basketItem - object of class \Bitrix\Sale\BasketItem $collection = $basketItem->getPropertyCollection(); $property = $collection->getItemById(123); $property->setField('VALUE', 'new value'); 'NAME', // property name 'VALUE', // property value 'CODE', // property code 'SORT', // sorting 'XML_ID' // external code
Deleting a property
// $basketItem - object of class \Bitrix\Sale\Basket $collection = $basketItem->getPropertyCollection(); $property = $collection->getItemById(123); $property->delete();
Redefine all available properties
Метод \Bitrix\Sale\BasketPropertiesCollection::redefine
operates as follows:
- when property is present in a collection, but is missing in the input data, the property will be deleted
- when property is present in a collection as well as in the input data, the property will be updated
- when property is missing in collection, but is present in the input data, the property will be added
// $basket - object of class \Bitrix\Sale\BasketBase foreach ($basket as $basketItem) { $collection = $basketItem->getPropertyCollection(); $collection->redefine([ [ 'NAME' => 'New property', 'CODE' => 'XXX', 'VALUE' => 'Value', ] ]); }
© «Bitrix24», 2001-2024