Перейти к основному контенту

Пример формирование YML фида

require_once($_SERVER['DOCUMENT_ROOT']. "/bitrix/modules/main/include/prolog_before.php");
header("Content-Type: text/xml");
header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");

$IBLOCK_ID = 26;

// получения всех сайтов
$rsSites = CSite::GetList($by="sort", $order="desc", ['ACTIVE' => 'Y']);

// Исключить характеристики
$specificationsArr = array(
  'Рейтинг для микроразметки',
  'Картинки',
  'Похожие товары по фильтру',
  'Аксессуары по фильтру',
  'Товары комплекта',
  'Видео (код из Youtube)',
  'Сертификаты',
  'ID поста блога для комментариев',
  'Регион'
);

// https://site.com/bitrix/admin/cat_group_admin.php?lang=ru
// Розничная цена МСК - 1
// Розничная цена Ростов-на-Дону - 7
// Розничная цена Краснодар - 10
// Розничная цена СПБ - 16
// Розничная цена Новосибирск - 19
// Розничная цена Казань - 13


$text = '<yml_catalog date="'.date('Y-m-d H:i:s').'">';
while ($arSite = $rsSites->Fetch()){

  // site.com
  // spb.site.com
  // rostov.site.com
  // krasnodar.site.com

  if (empty($arSite['SITE_URL'])) {
    $arSite['SITE_URL'] = 'https://site.com';
  }

  if ($arSite['SITE_URL'] == 'https://site.com' OR $arSite['SITE_URL'] == 'https://spb.site.com' OR $arSite['SITE_URL'] == 'https://rostov.site.com' OR $arSite['SITE_URL'] == 'https://krasnodar.site.com' ) {

  $category_query = CIBlockSection::GetList( Array('name' => 'asc'), Array('IBLOCK_ID' => $IBLOCK_ID, 'ACTIVE' => 'Y') );
  $product_query = CIBlockElement::GetList( array("ID" => "ASC"), array("IBLOCK_ID" => $IBLOCK_ID, "ACTIVE"=>"Y"), false, Array(), array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "NAME", "DETAIL_TEXT", "DETAIL_PICTURE","DETAIL_PAGE_URL", "DATE_ACTIVE_FROM", "PROPERTY_*") );

    if ($arSite['SITE_URL'] == 'https://site.com' ) {
      $PRICE_TYPE_ID = 1;
    } elseif ($arSite['SITE_URL'] == 'https://novosibirsk.site.com'){
      $PRICE_TYPE_ID = 19;
    } elseif ($arSite['SITE_URL'] == 'https://rostov.site.com'){
      $PRICE_TYPE_ID = 7;
    } elseif ($arSite['SITE_URL'] == 'https://krasnodar.site.com'){
      $PRICE_TYPE_ID = 10;
    } elseif ($arSite['SITE_URL'] == 'https://kazan.site.com'){
      $PRICE_TYPE_ID = 13;
    } elseif ($arSite['SITE_URL'] == 'https://spb.site.com'){
      $PRICE_TYPE_ID = 16;
    }

  $text .= '<shop>';
  $text .= '<name>'.$arSite['NAME'].'</name>';
  $text .= '<company>КНАУФ Инсулейшн</company>';
  $text .= '<url>'.$arSite['SITE_URL'].'</url>';
  $text .= '<platform>BSM/Yandex/Market</platform>';
  $text .= '<version>2.3.4</version>';
  $text .= '<cpa>1</cpa>';
  $text .= '<currencies><currency id="RUR" rate="1"/></currencies>';

  $text .= '<categories>';
  while ($category = $category_query->GetNext()) {
    $text .= '<category id="'.$category['ID'].'">'.$category['NAME'].'</category>';
  }
  $text .= '</categories>';
  $text .= '<enable_auto_discounts>true</enable_auto_discounts>';
  $text .= '<offers>';

  while($product = $product_query->GetNextElement()){ 
    $productArFields = $product->GetFields();  
    $productArProps = $product->GetProperties();

    // Определяем цену 
    $price_qury = CPrice::GetList(array(), array( "PRODUCT_ID" => $productArFields['ID'], "CATALOG_GROUP_ID" => $PRICE_TYPE_ID ));

    if ($price = $price_qury->Fetch()) {
      // Определяем категорию к которой относиться товар
      $category = CIBlockSection::GetByID($productArFields['IBLOCK_SECTION_ID']);
      $category_ID = $category->GetNext();

      $text .= '<offer id="'.$productArFields['ID'].'" available="true">';
      $text .= '<url>'.$arSite['SITE_URL'].$productArFields['DETAIL_PAGE_URL'].'</url>';
      $text .= '<price>'.substr($price["PRICE"], 0, -3).'</price>';
      $text .= '<currencyId>RUR</currencyId>';
      $text .= '<categoryId>'.$category_ID['ID'].'</categoryId>';

      $text .= '<picture>'.$arSite['SITE_URL'].CFile::GetPath($productArFields['DETAIL_PICTURE']).'</picture>';

      foreach($productArProps['MORE_PHOTO']['VALUE'] as $picture){
        $text .= '<picture>'.$arSite['SITE_URL'].CFile::GetPath($picture).'</picture>';
      }

      $text .= '<name>'.$productArFields['NAME'].'</name>';
      $text .= '<description><![CDATA['.$productArFields['DETAIL_TEXT'].']]></description>';

      foreach($productArProps as $specifications){
        if ($specifications['VALUE'] AND !in_array($specifications['NAME'], $specificationsArr)){

          if ($specifications['NAME'] == "Материал"){
            $text .= '<param name="'.$specifications['NAME'].'">'.$specifications['VALUE'][0].'</param>';
          } elseif ($specifications['NAME'] == "Преимущества") {
            $text .= '<param name="'.$specifications['NAME'].'"><![CDATA['.$specifications['VALUE']['TEXT'].']]></param>';
          } else {
            $text .= '<param name="'.$specifications['NAME'].'">'.$specifications['VALUE'].'</param>';
          }

        }
      }

      $text .= '</offer>';
    }
    // break;
  }


  $text .= '</offers>';
  $text .= '</shop>';
  // break;
  // die();
}
}

$text .= '</yml_catalog>';

echo $text;