Skip to content

Instantly share code, notes, and snippets.

@andrei99
Last active March 12, 2021 13:57
Show Gist options
  • Save andrei99/11362e04bb1fda3a4a7fc9f6fc6613b6 to your computer and use it in GitHub Desktop.
Save andrei99/11362e04bb1fda3a4a7fc9f6fc6613b6 to your computer and use it in GitHub Desktop.
init.php
\Bitrix\Main\Loader::includeModule('iblock');
\Bitrix\Main\Loader::includeModule('catalog');
//Добавляем реальный путь к товару для подписки
AddEventHandler('main', 'OnBeforeEventSend', Array("ChangeProductSubscribe", "SubscribeOnBeforeEventSend"));
class ChangeProductSubscribe
{
function SubscribeOnBeforeEventSend(&$arFields, &$arTemplate)
{
if ($arTemplate['ID'] == 28) {
//ИБ товаров
$intIBlockID = 1;
//получаем ID товара по ТП
$intSkuID = $arFields['ITEM_ID'];
$arProductProp = \CCatalogSku::GetProductInfo($intSkuID);
//получаем DETAIL_PAGE_URL
if (is_numeric($arProductProp['ID'])) {
$strProductUrl = '';
$arSelect = array(
"IBLOCK_ID",
"ID",
"DETAIL_PAGE_URL"
);
$arFilter = array(
"IBLOCK_ID" => $intIBlockID,
"ID" => $arProductProp['ID'],
);
$res = \CIBlockElement::GetList(
array(),
$arFilter,
false,
array("nPageSize" => 1),
$arSelect
);
while ($ob = $res->GetNextElement()) {
$arFieldsList = $ob->GetFields();
$strProductUrl = $arFieldsList['DETAIL_PAGE_URL'];
}
//Пишем DETAIL_PAGE_URL в шаблон письма о пуступлении товара
if(!empty($strProductUrl)){
$arFields['FULL_DETAIL_PAGE'] = $_SERVER['SERVER_NAME'] . $strProductUrl;
}
}
}
}
}
//add all product property min price
<?
$arResult = array();
$arSelect = array(
"IBLOCK_ID",
"ID",
"NAME"
);
$arFilter = array(
"IBLOCK_ID"=>1
);
$res = CIBlockElement::GetList(
array("ID" => "ASC"),
$arFilter,
false,
array(),
$arSelect
);
while($ob = $res->GetNextElement())
{
$arFields = $ob->GetFields();
$arResult[] = $arFields;
}
if(!empty($arResult)){
foreach ($arResult as $arItem){
$intProductID = $arItem['ID'];
if(is_numeric($intProductID)){
$arOffersList = \CCatalogSKU::getOffersList($intProductID, 1, array(), array('CATALOG_QUANTITY', 'CATALOG_PRICE_1'), array());
$intMinPrice = 0;
if(!empty($arOffersList)){
foreach ($arOffersList[$intProductID] as $arOffer){
if($intMinPrice == 0){
$intMinPrice = $arOffer['CATALOG_PRICE_1'];
continue;
}
if($intMinPrice > $arOffer['CATALOG_PRICE_1']){
$intMinPrice = $arOffer['CATALOG_PRICE_1'];
}
}
}
CIBlockElement::SetPropertyValuesEx($intProductID, 1, array('MIN_PRICE' => intval($intMinPrice)));
}
}
}
?>
<?php
use Bitrix\Main\Loader;
use Bitrix\Main\Diag\Debug;
Loader::includeModule('iblock');
Loader::includeModule('catalog');
AddEventHandler('iblock', 'OnAfterIBlockElementAdd', 'setProductMinPrice');
AddEventHandler('iblock', 'OnAfterIBlockElementUpdate', 'setProductMinPrice');
if (!function_exists('setProductMinPrice')) {
function setProductMinPrice(&$arFields)
{
$intProductID = $arFields['ID'];
//Debug::writeToFile($arFields, '$arFields', '__product_add.log');
if(is_numeric($intProductID)){
$arOffersList = \CCatalogSKU::getOffersList($intProductID, 1, array(), array('CATALOG_QUANTITY', 'CATALOG_PRICE_1'), array());
$intMinPrice = 0;
if(!empty($arOffersList)){
foreach ($arOffersList[$intProductID] as $arOffer){
if($intMinPrice == 0){
$intMinPrice = $arOffer['CATALOG_PRICE_1'];
continue;
}
if($intMinPrice > $arOffer['CATALOG_PRICE_1']){
$intMinPrice = $arOffer['CATALOG_PRICE_1'];
}
}
}
CIBlockElement::SetPropertyValuesEx($intProductID, 1, array('MIN_PRICE' => intval($intMinPrice)));
}
}
}
\Bitrix\Main\Loader::includeModule('iblock');
//Делаем не активным товар, если нет предложений
AddEventHandler("iblock", "OnAfterIBlockElementUpdate", Array("UpdateElementIBlock", "OnAfterIBlockElementUpdateHandler"));
class UpdateElementIBlock
{
function OnAfterIBlockElementUpdateHandler(&$arFields)
{
//ИБ товаров
$intIBlockID = 1;
if($arFields["RESULT"] && $arFields['IBLOCK_ID'] == $intIBlockID){
$intProductID = $arFields['ID'];
if(is_numeric($intProductID)){
//получаем все ТП товара $intProductID
$arOffersList = \CCatalogSKU::getOffersList($intProductID, $intIBlockID, array(), array('CATALOG_QUANTITY'), array());
if(is_array($arOffersList)){
$blDisabledSKU = true;
foreach ($arOffersList[$intProductID] as $arOffer){
if($arOffer['CATALOG_QUANTITY'] > 0){
$blDisabledSKU = false;
break;
}
}
//пишем св-во DISABLED_QUANTITY_SKU
if($blDisabledSKU){
$mixPropValue = 121;
}else{
$mixPropValue = false;
}
CIBlockElement::SetPropertyValuesEx($intProductID, $intIBlockID, array('DISABLED_QUANTITY_SKU' => $mixPropValue));
}
}
}
}
}
//change title by all site, out section 6,7 and includes section
AddEventHandler('main', 'OnEndBufferContent', 'setTitlePage');
function setTitlePage(&$strContent)
{
if (SITE_ID == 's3' && $_REQUEST['CODE'] !== 'tablet_pc' && $_REQUEST['CODE'] !== 'phone')
{
$strRequestCode = $_REQUEST['CODE'];
//by element
$arItemProp = array();
$arSelect = array(
"IBLOCK_ID",
"ID",
"IBLOCK_SECTION_ID"
);
$arFilter = array(
"IBLOCK_ID" => 3,
"ACTIVE_DATE" => "Y",
"ACTIVE" => "Y",
"CODE" => $strRequestCode
);
$res = CIBlockElement::GetList(
array("RAND" => "ASC"),
$arFilter,
false,
array("nPageSize" => 1),
$arSelect
);
while ($ob = $res->GetNextElement()) {
$arFields = $ob->GetFields();
$arItemProp[] = $arFields;
}
$intSearchParentID = $arItemProp[0]['IBLOCK_SECTION_ID'];
//by section
if(empty($arItemProp) && !empty($strRequestCode))
{
$SectList = CIBlockSection::GetList(
array("RAND" => "ASC"),
array(
"IBLOCK_ID"=>3,
"ACTIVE"=>"Y",
"CODE" => $strRequestCode
),
false,
array(
"ID",
)
);
while ($SectListGet = $SectList->GetNext())
{
$arSectionProp[]= $SectListGet;
break;
}
$intSearchParentID = $arSectionProp[0]['ID'];
}
//parent section
$arSectionParent = array();
$navChain = CIBlockSection::GetNavChain(3, $intSearchParentID);
while ($arNav=$navChain->GetNext()){
$arSectionParent[] = $arNav;
break;
}
//add title
if(empty($strRequestCode) || ($arSectionParent[0]['ID'] != 6 && $arSectionParent[0]['ID'] != 7))
{
preg_match('#<title>(.*?)</title>#',$strContent,$arSearchFound);
$strContent = preg_replace('#<title>(.*?)</title>#', '<title>'. $arSearchFound[1] .'. Санкт-Петербург</title>', $strContent);
}
}
}
AddEventHandler('iblock', 'OnAfterIBlockElementAdd', 'setNotifyReviewCrm');
function setNotifyReviewCrm(&$arFields)
{
if($arFields['IBLOCK_ID'] == REVIEW_IBLOCK_ID){
$strPathWebHook = 'https://you_domain.bitrix24.ru/rest/6/CODE_WEB_HOOK/';
$strQueryUrl = $strPathWebHook . 'im.notify.system.add';
// REST Api
$queryData = http_build_query(array(
'USER_ID' => 6,
'MESSAGE' => 'Message custom',
'MESSAGE_OUT' => '',
'TAG' => '',
'SUB_TAG' => '',
'ATTACH' => Array()
));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $strQueryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
$strResult = curl_exec($curl);
curl_close($curl);
// Debug::writeToFile($strResult, '$strResult', '__test_log123.log');
//https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=93&LESSON_ID=12131
}
}
//canonical
use \Bitrix\Main\Application;
AddEventHandler('main', 'OnEndBufferContent', 'setCanonical');
if (!function_exists('setCanonical')) {
function setCanonical(&$strContent)
{
$request = Application::getInstance()->getContext()->getRequest();
if (!$request->isAdminSection()) {
$strContent = str_replace('#SERVER_HOST_CANONICAL#', $_SERVER['HTTP_HOST'], $strContent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment