<?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;?>
<?}?>
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;
}
}