Answer the question
In order to leave comments, you need to log in
Bitrix. How to find out if a product has a gift in the product list?
Hello. The task is to display the "Gift" icon if gifts are attached to the product. Gifts are connected through the basket rule in the admin panel in the "Marketing" section. In the product card, I display this icon like this:
<?if ($arResult['CATALOG'] && $arParams['USE_GIFTS_DETAIL'] == 'Y' && \Bitrix\Main\ModuleManager::isModuleInstalled("sale")){?>
<?
$foo = new DiscountsHelper();
$funcname = "getGiftIds";
$isad = $foo->$funcname($arResult["ID"]);
?>
<?if(!empty($isad)):?>
<div class="gift-wrap"></div>
<?endif;?>
<?}?>
Answer the question
In order to leave comments, you need to log in
Understood it was necessary to insert
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