The last notes
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:
Comments