C
C
ChertyakaNUB2020-09-16 15:51:13
1C-Bitrix
ChertyakaNUB, 2020-09-16 15:51:13

How do I change the product name when adding a product to the cart?

Hey!
I add a product (by trading application ID) using the code:

$Cart = Sale\Basket::loadItemsForFUser(Sale\Fuser::getId(), Bitrix\Main\Context::getCurrent()->getSite());
$item = $Cart->createItem('catalog', 6667);
          $item->setFields(array(
            'NAME' => 'Новое название - '.$Price,
            'CURRENCY' => 'RUB',
            'NOTES' => $Price,
            'QUANTITY' => 1,
            'LID' => Bitrix\Main\Context::getCurrent()->getSite(),
            'PRICE' => -(int)$Price,
            'CUSTOM_PRICE' => 'Y',
            
          ));
          $Cart->save();

After adding, the product appears in the cart, the price changes, but the name of the product does not.
How can the name be changed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
ChertyakaNUB, 2020-11-25
@ChertyakaNUB

Solution: Forget about Bitrix as a mountain of development by shit developers from Russia and use normal frameworks like Laravel!

S
Sergey, 2021-01-13
@Logic87

\Bitrix\Main\EventManager::getInstance()->addEventHandler(
    'sale',
    'OnSaleBasketItemBeforeSaved',
    'OnSaleBasketItemBeforeSavedHandler'
);

function OnSaleBasketItemBeforeSavedHandler(\Bitrix\Main\Event $event) {
    $basketItem = $event->getParameter('ENTITY');
    $oldValues = $event->getParameter("VALUES");
     //пересохраняем название товара если оно было изменено обработчиком при добавлении в корзину
     if (!empty($oldValues["NAME"])) {
           $basketItem->setField('NAME', $oldValues["NAME"]);
     }
    return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question