P
P
photosho2017-05-22 16:31:31
1C-Bitrix
photosho, 2017-05-22 16:31:31

How to programmatically reduce the number of items in the cart?

How to programmatically reduce the number of items in the cart? Does not allow to reduce due to the binding of goods to shipments. I use the "CSaleBasket::Update" method.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Burlaka, 2017-05-23
@photosho

Alexey Emelyanov :

You probably need to delete the shipping document first.

When updating through the admin panel, the documents are not a hindrance (they remain posted, a clarification appears on how much has been shipped and how much needs to be shipped ).
You also need to be careful, because the gifts used during creation "jump off". Didn't check discounts.
Use D7 .
Working code
( inside - PHPDocs, without which the IDE cannot navigate the hints ):
// Читаем заказ, читаем корзину
$order= \Bitrix\Sale\Order::load( 6924 );
$basket= $order->getBasket();
$basket_items= $basket->getBasketItems();

/**
 * @var $bi \Bitrix\Sale\BasketItem
 */
foreach ( $basket_items as $bi ) {
  $quantity_current= $bi->getField( 'QUANTITY' );
  
  //Приходит в формате "3.000"
  $quantity_current++;
  $quantity_current= $bi->setField( 'QUANTITY', $quantity_current );
  
  $save_result= $bi->save();
  if ( !$save_result->isSuccess() ) {
    ShowError( $save_result->getErrorMessages() );
    exit;
  }
}

(!) When changing in this way, the history of changes in the accompanying information to the order is also accumulated.
In particular: change in the order quantity, order value, changes in gifts .
Also, in shipments, the counter of the required pre-shipment works correctly.

A
Alexey Emelyanov, 2017-05-22
@babarun

Does not allow to reduce due to the binding of goods to shipments
- show the exact wording of the error that the api returns.
Is the shopping cart already linked to the order? You probably need to delete the shipping document first.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question