A
A
Andrey Eskov2017-10-03 13:09:01
1C-Bitrix
Andrey Eskov, 2017-10-03 13:09:01

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

1 answer(s)
A
Artyom Luchnikov, 2017-10-03
@taurus2790

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

The bottom line is that we create an empty pseudo-basket and, based on it, determine whether there are gifts for the transferred goods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question