E
E
Evgeny Nikolaev2019-05-23 17:45:21
1C-Bitrix
Evgeny Nikolaev, 2019-05-23 17:45:21

In the bitrix cart, I use a self-written ajax api on CSaleBasket::Update through which the number of goods is not always updated. Cash?

A similar question was asked at the link The basket is not updated by the CSaleBasket::Update method on 1c bitrix? did not see an answer to it.
I'll go into more detail about what's going on:
Self-written JS is used on the standard cart component. The update is carried out by Ajax
Here is the function that performs it:
function productUpdateInBasket($PRODUCT_ID=0,$QUANTITY=0) {
//// Execute a request to the basket to find out if it has an item with Product_ID
$a = CSaleBasket::GetList(// We execute a request to the cart to find out if it has an item with a product_id
$arOrder = array(),
$arFilter = array("PRODUCT_ID"=>$PRODUCT_ID),
$arGroupBy = false,
$arNavStartParams = false,
$arSelectFields = array()
);
if (count($a->arResult)) {// If there is, we get the ID of this item in the basket (may not be the same as the Product ID)
$idProductInBasket = $a->arResult[0]["ID"];
$arFields_new = array("QUANTITY"=>$QUANTITY);
return CSaleBasket::Update($idProductInBasket, $arFields_new);
}
return false;
}
Sometimes some products stopped updating, that is, everything works in general. But sometimes, for some product added from the catalog in the cart, the quantity stops updating via ajax to the specified function.
Moreover, it stops updating, as it can be seen in the standard component and in the header, the information in which is requested via
CSaleBasket::GetList(//Performing the request
array("NAME" => "
array("FUSER_ID" => CSaleBasket::GetBasketUserID(), "LID" => SITE_ID, "ORDER_ID" => "NULL"),
false,
false,
array("ID","MODULE","PRODUCT_ID"," QUANTITY","CAN_BUY","PRICE"));
The situation is corrected if you click to remove the product and re-add it.
Moreover, the delete button must be pressed 2-3 times and only after that the product is deleted.
To delete, use
function productDeleteFromBasket($PRODUCT_ID=0) {
//// Execute a request to the basket to find out if it has an item with Product_ID
$a = CSaleBasket::GetList(// Execute a request to the basket to find out if it has an item with Product_ID
$arOrder = array(),
$arFilter = array("
$arNavStartParams = false,
$arSelectFields = array()
);
if (count($a->arResult)) {// If there is, we get the ID of this item in the basket (may not be the same as the Product ID)
$idProductInBasket = $a->arResult[0]["ID"];
$arFields_new = array("QUANTITY"=>0);
return CSaleBasket::Update($idProductInBasket, $arFields_new);
}
return false;
}
What is it? Caching? Is it possible to somehow force CSaleBasket to update it?
Even when the quantity is not updated return CSaleBasket::Update($idProductInBasket, $arFields_new); the method returns a positive result without errors.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Nikolaev, 2020-12-05
@nikolaevevge

Replaced the function code with the following:

function productUpdateInBasket($prodId=0,$QUANTITY=0) {
    if ($prodId == 0) {return false;}
      $basket = \Bitrix\Sale\Basket::loadItemsForFUser(\Bitrix\Sale\Fuser::getId(CSaleBasket::GetBasketUserID()), \Bitrix\Main\Context::getCurrent()->getSite());
      $basket->refresh();

      $dbRes = \Bitrix\Sale\Basket::getList(array(
        "select"=>["PRODUCT_ID","NAME","QUANTITY","ID"],
        "filter"=>array(
        "=FUSER_ID"=>\Bitrix\Sale\Fuser::getId(),
        "=ORDER_ID" => null,
        "=LID"=>\Bitrix\Main\Context::getCurrent()->getSite(),
        "=CAN_BUY"=>"Y",
        "=PRODUCT_ID"=>$prodId
      )
    ));

    $itemId = false;

    while ($item = $dbRes->fetch()) {
      if (isset($item["ID"]) and $item["ID"] and isset($item["PRODUCT_ID"]) and ($item["PRODUCT_ID"] == $prodId)) {
        $itemId = $item["ID"];
      }
    }

    if ($itemId) {
      $itemProd = $basket->getItemById($itemId);
      if ($QUANTITY==0) {
        $itemProd->delete();
      } else {
        $itemProd->setField("QUANTITY",$QUANTITY);
      }
      $basket->save();
      $basket->refresh();
      return true;
   }
  return false;
}

So more infa with code examples on the situation here blog.ivru.net/?id=51

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question