Answer the question
In order to leave comments, you need to log in
How to create a user cart using API without authorization, without $USER for any user?
API function call code that leads to the problem:
Loader::includeModule('catalog');
Loader::includeModule('sale');
arFUser = CSaleUser::GetList(['USER_ID' => $userId]);
$basket = Sale\Basket::loadItemsForFUser($arFUser['ID'], SITE_ID);
if ($item = $basket->getExistsItem('catalog', $productId)) {
$item->setField('QUANTITY', $item->getQuantity() + $quantity);
} else {
$item = $basket->createItem('catalog', $productId);
//функция которая вызывает ошибку "setFields"
$item->setFields([
'QUANTITY' => $quantity,
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'LID' => SITE_ID,
'PRODUCT_PROVIDER_CLASS' => \Bitrix\Catalog\Product\Basket::getDefaultProviderName(),
]);
$basketPropertyCollection = $item->getPropertyCollection();
$basketPropertyCollection->setProperty($arProps);
}
$saveResult = $basket->save();
if(!$saveResult->isSuccess())
$arError[] = implode(', ', $saveResult->getErrors());
public function getContext() {
global $USER;
$context = array();
/** @var BasketItem $basketItem */
$basketItem = $this->rewind();
if ($basketItem)
{
$siteId = $basketItem->getField('LID');
$fuserId = $basketItem->getFUserId();
$currency = $basketItem->getCurrency();
$userId = Fuser::getUserIdById($fuserId);
if (empty($context['SITE_ID']))
{
$context['SITE_ID'] = $siteId;
}
if (empty($context['USER_ID']) && $userId > 0)
{
$context['USER_ID'] = $userId;
}
if (empty($context['CURRENCY']) && !empty($siteId))
{
if (empty($currency))
{
$currency = Internals\SiteCurrencyTable::getSiteCurrency($siteId);
}
if (!empty($currency) && Currency\CurrencyManager::checkCurrencyID($currency))
{
$context['CURRENCY'] = $currency;
}
}
}
if (empty($context['SITE_ID']))
{
$context['SITE_ID'] = SITE_ID;
}
if (empty($context['USER_ID']))
{
$context['USER_ID'] = $USER->GetID() > 0 ? $USER->GetID() : 0;
}
if (empty($context['CURRENCY']))
{
Loader::includeModule('currency');
$context['CURRENCY'] = Currency\CurrencyManager::getBaseCurrency();
}
return $context;
}
Answer the question
In order to leave comments, you need to log in
Your call comes from init.php, at this stage there is no global object of the CUser class yet, it is created later, it is better to use the OnPageStart event.
But you can use the well-known "crutch", but this is not entirely correct,
global $USER;
$USER = new CUser();
And you will have a working instance of $USER, and the GetID method will return NULL, but there will be no more errors, and further along the code, the class will be re-initialized already by Bitrix as usual.
$userId = ...;
$fuserId = \Bitrix\Sale\Fuser::getIdByUserId($userID);
$basket = \Bitrix\Sale\Basket::loadItemsForFUser($fuserId , Bitrix\Main\Context::getCurrent()->getSite());
You cannot create an order without having a user in the system while maintaining the normal operation of the system.
1C-Bitrix normally works only in two options:
1) You register a user and place an order for him
2) You must require authorization from the user before creating an order
As you can see, authorization is checked at a lower level and you will not be able to do it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question