Answer the question
In order to leave comments, you need to log in
How to check if a product has gifts in Bitrix?
Dear, tell me how can I check for the presence of a gift for the product in the product card?
Are there ready-made methods in bitrix api?
Answer the question
In order to leave comments, you need to log in
I once wrote a method that, by product id, returns an array of id gifts. You can use:
use Bitrix\Sale\Compatible\DiscountCompatibility;
use Bitrix\Sale\Basket;
use Bitrix\Sale\Discount\Gift;
use Bitrix\Sale\Fuser;
class DiscountsHelper
{
/**
* Возвращает массив id всех доступных подарков для товара
*
* @param int $productId - идентификатор товара
* @return array - массив с id подарков для товара
*/
public static function getGiftIds($productId)
{
$giftProductIds = [];
if (!$productId) {
return $giftProductIds;
}
DiscountCompatibility::stopUsageCompatible();
$giftManager = Gift\Manager::getInstance();
$potentialBuy = [
'ID' => $productId,
'MODULE' => 'catalog',
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
'QUANTITY' => 1,
];
$basket = Basket::loadItemsForFUser(Fuser::getId(), SITE_ID);
$basketPseudo = $basket->copy();
foreach ($basketPseudo as $basketItem) {
$basketItem->delete();
}
$collections = $giftManager->getCollectionsByProduct($basketPseudo, $potentialBuy);
foreach ($collections as $collection) {
/** @var \Bitrix\Sale\Discount\Gift\Gift $gift */
foreach ($collection as $gift) {
$giftProductIds[] = $gift->getProductId();
}
}
DiscountCompatibility::revertUsageCompatible();
return $giftProductIds;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question