I
I
Ivan Ivanov2018-05-24 10:44:59
1C-Bitrix
Ivan Ivanov, 2018-05-24 10:44:59

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

in the list is not obtained .... Gives a php error. Tried to connect
use Bitrix\Main\Loader;
use Bitrix\Main\ModuleManager;

in the product list, but it does not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Ivanov, 2018-05-24
@ZZiliST

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 question

Ask a Question

731 491 924 answers to any question