Bitrix product subscription via API
Rus
Eng
Битрикс подписка на товар через API
I solve complex technical challenges for Bitrix and 1C—ranging from auditing existing code to website development and configuring complex integrations—with a guarantee of results.
Recommended

Website hosting

If you are choosing hosting for your project, I recommend Timeweb. Modern infrastructure, a wide range of plans from starter options to high-load VPS, and responsive tech support that actually helps instead of sending template replies. I host some of my own projects there and recommend it to my clients.

Go to site Affiliate link. The price remains the same for you.

All English-language materials have been translated fully automatically using the Google service

OEM component call code catalog.product.subscribe

<? $APPLICATION->IncludeComponent(
		'tichiy:catalog.product.subscribe',
		'riba',
		array(
			'CUSTOM_SITE_ID' => SITE_ID,
			'PRODUCT_ID' => $arItem['ID'],
			'BUTTON_ID' => $this->GetEditAreaId($arItem['ID']).'_subscribe',
			'BUTTON_CLASS' => 'btn btn-default product-item-detail-buy-button',
			'DEFAULT_DISPLAY' => !$actualItem['CAN_BUY'],
			'MESS_BTN_SUBSCRIBE' => 'Подписаться',
		),
		$component,
		array('HIDE_ICONS' => 'Y')
	);
?>

Subscription to a product in Bitrix

Adding a Product Subscription Using the API

$subscribeManager = new \Bitrix\Catalog\Product\SubscribeManager;
$contactTypes = $subscribeManager->contactTypes;

$subscribeData = array(
    'USER_CONTACT' => $USER->getEmail() ? $USER->getEmail() : false,
    'ITEM_ID' => 17715, //ID товара
    'SITE_ID' => 's1',
    'CONTACT_TYPE' => \Bitrix\Catalog\SubscribeTable::CONTACT_TYPE_EMAIL,
    'USER_ID' => $userId ? $userId : false,
);

$subscribeId = $subscribeManager->addSubscribe($subscribeData);

if($subscribeId){

}else{
    $errorObject = current($subscribeManager->getErrors());
    $errors = array('error' => true);
    if($errorObject)
    {
        $errors['message'] = $errorObject->getMessage();
        if($errorObject->getCode() == $subscribeManager::ERROR_ADD_SUBSCRIBE_ALREADY_EXISTS)
        {
            $errors['setButton'] = true;
        }
    }
}

The list of subscribers is in Store -> Buyers -> Subscribe to goods

Additional fields cannot be added to it.

Deleting a subscription to a product in Bitrix

$subscribeManager = new \Bitrix\Catalog\Product\SubscribeManager;
if(!$subscribeManager->deleteManySubscriptions($_POST['listSubscribeId'], $_POST['itemId']))
{
    $errorObject = current($subscribeManager->getErrors());
    if($errorObject) {
        $errors = $errorObject->getMessage();
    }
}

Checking if the user is subscribed to the product

use Bitrix\Catalog\SubscribeTable;
use Bitrix\Catalog\Product\SubscribeManager;

global $USER;

foreach ($arResult['ITEMS'] as $k => &$item) {
	$userId = $USER->GetID();

	$filter = array(
		'ITEM_ID' => $item['ID'],
		'=SITE_ID' => SITE_ID,
		array(
			'LOGIC' => 'OR',
			array('=DATE_TO' => false),
			array('>DATE_TO' => date($DB->dateFormatToPHP(\CLang::getDateFormat('FULL')), time()))
		)
	);
	if ($userId)
		$filter['USER_ID'] = $userId;
	else{
		if (!empty($_SESSION['SUBSCRIBE_PRODUCT']['TOKEN']) && !empty($_SESSION['SUBSCRIBE_PRODUCT']['USER_CONTACT'])){
			$filter['=Bitrix\Catalog\SubscribeAccessTable:SUBSCRIBE.TOKEN'] = $_SESSION['SUBSCRIBE_PRODUCT']['TOKEN'];
			$filter['=Bitrix\Catalog\SubscribeAccessTable:SUBSCRIBE.USER_CONTACT'] = $_SESSION['SUBSCRIBE_PRODUCT']['USER_CONTACT'];
		}else{
			item['IS_SUBSCRIBED'] = false;
		}
	}

	$queryObject = SubscribeTable::getList(array('select' => array('ITEM_ID'), 'filter' => $filter));
	$subscribeManager = new SubscribeManager;
	$listRealItemId = array();
	while ($subscribe = $queryObject->fetch()) {
		$subscribeManager->setSessionOfSibscribedProducts($subscribe['ITEM_ID']);
		$listRealItemId[] = $subscribe['ITEM_ID'];
	}

	if (in_array($item['ID'], $listRealItemId))
		item['IS_SUBSCRIBED'] = true;
	else
		item['IS_SUBSCRIBED'] = false;
}

Intercept the event responsible for sending the notification about the change in the status of the goods

AddEventHandler('main', 'OnBeforeEventAdd', array('OnBeforeEventAdd', 'OnProductNotify'));
class OnBeforeEventAdd {
   static function OnProductNotify(&$event, &$lid, &$arFields, &$message_id) {
        if ($event == 'CATALOG_PRODUCT_SUBSCRIBE_NOTIFY') {
			// Your logic is here
       }
   }
}

Bitrix settings for product receipt notifications

  // Module Trade Catalog
Enable quantitative accounting - yes
Allow purchase in the absence of goods - no
Allow negative quantity - no
Allow subscription if there is no product - yes

// Module Online store
Store settings -> Subscribe to goods -> Use notifications on goods receipt - yes  

 

Worth reading:

Aleksandr Tichiy
Author

Aleksandr Tichiy

Fullstack developer · 1C-Bitrix

I build and modernize online stores and corporate portals on 1C-Bitrix. Integrations with 1C, high-load performance, 152-FZ compliance. Writing about architecture and e-commerce.


Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site