Answer the question
In order to leave comments, you need to log in
How to add a product to the cart when adding another product?
Hello colleagues!
After update 18.1.3, Add2BasketByProductID does not seem to work.
For certain products, you need to add another previously known product to the cart.
On the OnBeforeBasketAdd event, I use the Add2BasketByProductID method to add a product.
AddEventHandler("sale", "OnBeforeBasketAdd", Array("CSaleHandlers","OnBeforeBasketAddHandler") );
class CSaleHandlers{
function OnBeforeBasketAddHandler(&$arFields) {
$arID = array(896,897,898,899,900,901,902,903,904,906,1457,1458);
if( in_array($arFields["PRODUCT_ID"], $arID) ){
$pos = Add2BasketByProductID( 1027, 2, array(), array() );
AddMessage2Log('$pos = '.print_r($pos, true),'');
}
}
}
Answer the question
In order to leave comments, you need to log in
Evgeny Zhukov on the forum helped to solve this issue by advising to use the \Bitrix\Catalog\Product\Basket::addProduct method (for public components) and \Bitrix\Catalog\Product\Basket::addProductToBasket (for arbitrary, including administrative scripts)."
After function Add2BasketByProductID try to add
if ($ex = $APPLICATION->GetException()){
\Bitrix\Main\Diag\Debug::dumpToFile($ex->GetString());
}
$product = array(
'PRODUCT_ID' => $productId,
'QUANTITY' => $quantity
);
/*@var $basketResult Bitrix\Main\Result*/
$basketResult = Catalog\Product\Basket::addProduct($product);
if (!$basketResult->isSuccess()) {
\Bitrix\Main\Diag\Debug::dumpToFile(implode('; ', $basketResult->getErrorMessages()));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question