Title
<?php
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin.php');
// Protect script
if (!$USER->IsAdmin()) {
die('Access denied');
}
use Bitrix\Main\Loader;
use Bitrix\Iblock\ElementTable;
use Bitrix\Main\Entity\Query;
Loader::includeModule('iblock');
// Get all products from catalog (IBLOCK_ID = 11)
$rsProducts = CIBlockElement::GetList(
array('ID' => 'ASC'),
array('IBLOCK_ID' => 11, 'ACTIVE' => 'Y'),
false,
false,
array('ID', 'NAME', 'IBLOCK_SECTION_ID', 'DETAIL_TEXT', 'PROPERTY_CML2_ARTICLE')
);
$updated = 0;
$processed = 0;
while ($product = $rsProducts->Fetch()) {
$processed++;
// Get section name
$sectionName = '';
if ($product['IBLOCK_SECTION_ID']) {
$rsSection = CIBlockSection::GetByID($product['IBLOCK_SECTION_ID']);
if ($arSection = $rsSection->Fetch()) {
$sectionName = $arSection['NAME'];
}
}
// Get price
$arFilter = array('IBLOCK_ID' => 12, 'ACTIVE' => 'Y');
$arSelect = array('ID', 'PROPERTY_CML2_LINK');
$rsOffers = CIBlockElement::GetList(array(), array_merge($arFilter, array('PROPERTY_CML2_LINK' => $product['ID'])), false, false, $arSelect);
$price = '';
if ($offer = $rsOffers->Fetch()) {
$rsPrice = CPrice::GetBasePrice($offer['ID']);
if ($arPrice = $rsPrice->Fetch()) {
$price = round($arPrice['PRICE']);
}
}
// Generate META TITLE
$metaTitle = generateTitle($product['NAME'], $sectionName, $price);
// Generate META DESCRIPTION
$metaDescription = generateDescription($product['NAME'], $sectionName, $product['DETAIL_TEXT']);
// Generate META KEYWORDS
$metaKeywords = generateKeywords($product['NAME'], $sectionName);
// Update in database
$el = new CIBlockElement;
$el->Update($product['ID'], array(
'DESCRIPTION' => json_encode(array(
'TITLE' => $metaTitle,
'DESCRIPTION' => $metaDescription,
'KEYWORDS' => $metaKeywords
))
));
$updated++;
if ($processed % 100 == 0) {
echo "Обработано: $processed<br>";
flush();
}
}
echo "<h2>Завершено!</h2>";
echo "Всего обработано товаров: $processed<br>";
echo "Обновлено: $updated<br>";
function generateTitle($name, $section, $price) {
$title = $name;
if ($section) {
$title .= " | " . $section;
}
if ($price) {
$title .= " | " . $price . " руб.";
}
$title .= " | 24weld.ru";
// Max 60 chars for SEO
return substr($title, 0, 60);
}
function generateDescription($name, $section, $detailText) {
$text = substr(strip_tags($detailText), 0, 100);
if (!$text) {
$text = "Товар: " . $name;
}
$desc = $name . ". " . $text . ". Купить на 24weld.ru";
// Max 160 chars for SEO
return substr($desc, 0, 160);
}
function generateKeywords($name, $section) {
$keywords = array();
$words = explode(' ', $name);
foreach ($words as $word) {
if (strlen($word) > 3) {
$keywords[] = mb_strtolower($word);
}
}
if ($section) {
$keywords[] = mb_strtolower($section);
}
$keywords[] = '24weld';
$keywords[] = 'купить';
return implode(', ', array_slice($keywords, 0, 8));
}
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php');
?>
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin.php');
// Protect script
if (!$USER->IsAdmin()) {
die('Access denied');
}
use Bitrix\Main\Loader;
use Bitrix\Iblock\ElementTable;
use Bitrix\Main\Entity\Query;
Loader::includeModule('iblock');
// Get all products from catalog (IBLOCK_ID = 11)
$rsProducts = CIBlockElement::GetList(
array('ID' => 'ASC'),
array('IBLOCK_ID' => 11, 'ACTIVE' => 'Y'),
false,
false,
array('ID', 'NAME', 'IBLOCK_SECTION_ID', 'DETAIL_TEXT', 'PROPERTY_CML2_ARTICLE')
);
$updated = 0;
$processed = 0;
while ($product = $rsProducts->Fetch()) {
$processed++;
// Get section name
$sectionName = '';
if ($product['IBLOCK_SECTION_ID']) {
$rsSection = CIBlockSection::GetByID($product['IBLOCK_SECTION_ID']);
if ($arSection = $rsSection->Fetch()) {
$sectionName = $arSection['NAME'];
}
}
// Get price
$arFilter = array('IBLOCK_ID' => 12, 'ACTIVE' => 'Y');
$arSelect = array('ID', 'PROPERTY_CML2_LINK');
$rsOffers = CIBlockElement::GetList(array(), array_merge($arFilter, array('PROPERTY_CML2_LINK' => $product['ID'])), false, false, $arSelect);
$price = '';
if ($offer = $rsOffers->Fetch()) {
$rsPrice = CPrice::GetBasePrice($offer['ID']);
if ($arPrice = $rsPrice->Fetch()) {
$price = round($arPrice['PRICE']);
}
}
// Generate META TITLE
$metaTitle = generateTitle($product['NAME'], $sectionName, $price);
// Generate META DESCRIPTION
$metaDescription = generateDescription($product['NAME'], $sectionName, $product['DETAIL_TEXT']);
// Generate META KEYWORDS
$metaKeywords = generateKeywords($product['NAME'], $sectionName);
// Update in database
$el = new CIBlockElement;
$el->Update($product['ID'], array(
'DESCRIPTION' => json_encode(array(
'TITLE' => $metaTitle,
'DESCRIPTION' => $metaDescription,
'KEYWORDS' => $metaKeywords
))
));
$updated++;
if ($processed % 100 == 0) {
echo "Обработано: $processed<br>";
flush();
}
}
echo "<h2>Завершено!</h2>";
echo "Всего обработано товаров: $processed<br>";
echo "Обновлено: $updated<br>";
function generateTitle($name, $section, $price) {
$title = $name;
if ($section) {
$title .= " | " . $section;
}
if ($price) {
$title .= " | " . $price . " руб.";
}
$title .= " | 24weld.ru";
// Max 60 chars for SEO
return substr($title, 0, 60);
}
function generateDescription($name, $section, $detailText) {
$text = substr(strip_tags($detailText), 0, 100);
if (!$text) {
$text = "Товар: " . $name;
}
$desc = $name . ". " . $text . ". Купить на 24weld.ru";
// Max 160 chars for SEO
return substr($desc, 0, 160);
}
function generateKeywords($name, $section) {
$keywords = array();
$words = explode(' ', $name);
foreach ($words as $word) {
if (strlen($word) > 3) {
$keywords[] = mb_strtolower($word);
}
}
if ($section) {
$keywords[] = mb_strtolower($section);
}
$keywords[] = '24weld';
$keywords[] = 'купить';
return implode(', ', array_slice($keywords, 0, 8));
}
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php');
?>
