Как выводить случайные товары API.6?

#
Как выводить случайные товары API.6?
Не могу сообразить, как правильно вывести несколько товаров случайным образом со всего интернет магазина, ичпользуя API 6?
По сути нужен синоним
$param['group_order']

Сейчас вывожу товары таким образом:

//Выводим случайные товары
$Shop_Controller_Show = new Shop_Controller_Show(Core_Entity::factory('Shop', 3));
$Shop_Controller_Show
   -> xsl(Core_Entity::factory('Xsl')
   -> getByName('СлучайныеТовары'))
   -> groupsMode('none')
   -> group(FALSE)
   -> itemsPropertiesList(FALSE)
   -> associatedItems(FALSE)
   -> limit(8)
   -> show();
#
Re: Как выводить случайные товары API.6?
Напишу как я делаю более узкую задачу по выводу случайных товаров из определенных групп (634, 636):


<?php
if (Core::moduleIsActive('shop'))
{
    $oShop = Core_Entity::factory('Shop', 1);
    
    // Выбрать всех потомков
    function getChildren($oParentShopGroup, $aGroups = array())
    {
        $aGroups[] = $oParentShopGroup->id;
        $aChildren = $oParentShopGroup->Shop_Groups->getAllByActive(1);
        foreach ($aChildren as $oChildrenGroup)
        {
            $aGroups = getChildren($oChildrenGroup, $aGroups);
        }
        return $aGroups;
    }

$aGroups1 = getChildren(Core_Entity::factory('Shop_Group', 634));
$aGroups2 = getChildren(Core_Entity::factory('Shop_Group', 636));
$aGroupsRes = array_merge((array)$aGroups1, (array)$aGroups2);
$Shop_Controller_Show = new Shop_Controller_Show(
    $oShop
);

$Shop_Controller_Show
->shopItems()
->queryBuilder()
->clearOrderBy()
->orderBy('RAND()')
->where('shop_items.shop_group_id', 'IN', $aGroupsRes);
->xsl(
  Core_Entity::factory('Xsl')->getByName('СлучайныеТовары')
  )
->groupsMode('tree')
->group(FALSE)
->limit(3)
->show();
}
?>
#
Re: Как выводить случайные товары API.6?
kovaldo, переписал Ваш код по себя, но не пойму в чём ошибка.
PHP:

$Shop_Controller_Show = new Shop_Controller_Show(Core_Entity::factory('Shop', 3));
$Shop_Controller_Show
   ->shopItems()
   ->queryBuilder()
   ->clearOrderBy()
   ->orderBy('RAND()')
   ->xsl(
      Core_Entity::factory('Xsl') -> getByName('СлучайныеТовары')
   )
   ->limit(5)
   ->show();

Хостайка выдаёт ошибку:
Exception: The method 'queryBuilder' does not exist in 'Shop_Controller_Show'
29 modules/core/exception.php
110 modules/core/servant/properties.php
22 templates/template16/template.htm
83 modules/template/model.php
100 modules/core/page.php
101 templates/template13/template.htm
83 modules/template/model.php
100 modules/core/page.php
363 modules/core/command/controller/default.php
188 modules/core/router/route.php
205 index.php

Меня смущает то, что в Вашем примере в классе Shop_Controller_Show мы вызываем методы, которые там не описаны:

  1. queryBuilder()
  2. clearOrderBy()
  3. orderBy('RAND()'
#
Re: Как выводить случайные товары API.6?
Эти методы делают сортировку товаров случайно.
Их кстати надо было отделить, поэтому и ошибка.
Правильно будет так:


         <?php
         if (Core::moduleIsActive('shop'))
         {
            $oShop = Core_Entity::factory('Shop', 1);

            // Выбрать всех потомков
            function getChildren($oParentShopGroup, $aGroups = array())
            {
               $aGroups[] = $oParentShopGroup->id;

               $aChildren = $oParentShopGroup->Shop_Groups->getAllByActive(1);
               foreach ($aChildren as $oChildrenGroup)
               {
                  $aGroups = getChildren($oChildrenGroup, $aGroups);
               }
               return $aGroups;
            }

            $aGroups1 = getChildren(Core_Entity::factory('Shop_Group', 634));
            $aGroups2 = getChildren(Core_Entity::factory('Shop_Group', 636));
            $aGroupsRes = array_merge((array)$aGroups1, (array)$aGroups2);

            $Shop_Controller_Show = new Shop_Controller_Show(
               $oShop
               );

            $Shop_Controller_Show
            ->shopItems()
            ->queryBuilder()
            ->clearOrderBy()
            ->orderBy('RAND()')
            ->where('shop_items.shop_group_id', 'IN', $aGroupsRes);

            $Shop_Controller_Show
            ->xsl(
               Core_Entity::factory('Xsl')->getByName('СлучайныеТовары')
               )
            ->groupsMode('tree')
            ->group(FALSE)
            ->limit(3)
            ->show();
         }

         ?>
#
Re: Как выводить случайные товары API.6?
Получилось. Спасибо за совет. Очень непривычно после коханы, надеюсь привыкну.
#
Re: Как выводить случайные товары API.6?
Возможно кому-то пригодится код для вывода случайных товаров интернет магазина:
PHP:
$Shop_Controller_Show = new Shop_Controller_Show(Core_Entity::factory('Shop', 3));
$Shop_Controller_Show
  ->shopItems()
  ->queryBuilder()
  ->clearOrderBy()
  ->orderBy('RAND()');
               
$Shop_Controller_Show
  ->xsl(
    Core_Entity::factory('Xsl') -> getByName('СлучайныеТовары')
  )
  ->limit(5)
  ->group(FALSE)
  ->show();
#
Re: Как выводить случайные товары API.6?
А как вывести случайные элементы из определенных групп ИС заменяю Shop на Informationsystem.
Все отдельно (без условий) работает, а с условиями не хочет.


<?php
$Structure_Controller_Show = new Structure_Controller_Show(
         Core_Entity::factory('Site', CURRENT_SITE));

     $structure_parent_id_array[] = $structure_parent_id = CURRENT_STRUCTURE_ID;

    while ($structure_parent_id != 0)
    {
       $oStructure = Core_Entity::factory('Structure', $structure_parent_id);
       $structure_parent_id = $oStructure->parent_id;
      $structure_parent_id_array[] = $structure_parent_id;
    }
?>   
   <div class="publicR publ_h1"><h1><a href="/press-center/information_materials/">ИНФОРМАЦИОННЫЕ МАТЕРИАЛЫ</a></h1></div>
<?php
if (Core::moduleIsActive('informationsystem')
{
     $oShop = Core_Entity::factory('informationsystem', 87);
    
     // Выбрать всех потомков
    function getChildren($oParentInformationsystemGroup, $aGroups = array())
     {
         $aGroups[] = $oParentInformationsystemGroup->id;
         $aChildren = $oParentInformationsystemGroup->Informationsystem_Groups->getAllByActive(1);
         foreach ($aChildren as $oChildrenGroup)
         {
             $aGroups = getChildren($oChildrenGroup, $aGroups);
         }
         return $aGroups;
     }

$aGroups1 = getChildren(Core_Entity::factory('Informationsystem_Group', 670));
$aGroups2 = getChildren(Core_Entity::factory('Informationsystem_Group', 668));
$aGroupsRes = array_merge((array)$aGroups1, (array)$aGroups2);

$Informationsystem_Controller_Show = new Informationsystem_Controller_Show(
     $oShop
);

  $Informationsystem_Controller_Show
->informationsystemItems()
->queryBuilder()
->clearOrderBy()
->orderBy('RAND()'
->where('informationsystem_items.informationsystem_group_id', 'IN', $aGroupsRes);

$Informationsystem_Controller_Show
->xsl(
   Core_Entity::factory('Xsl'->getByName('СписокЭлементовPUBL_second'
   )
->groupsMode('tree'
->group(FALSE)
->limit(3)
->itemsProperties(TRUE)
->itemsPropertiesList(FALSE);



if (CURRENT_STRUCTURE_ID != 580) {
$oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id'
->from('properties'
->where('properties.tag_name', 'LIKE', 'listIdStructure';

$Informationsystem_Controller_Show
->informationsystemItems()
->queryBuilder()
->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id'
->where('property_value_strings.property_id', 'IN', $oCore_QueryBuilder_Select2)
->open();

foreach ($structure_parent_id_array as $v) {
$Informationsystem_Controller_Show
->informationsystemItems()
->queryBuilder()
->where('property_value_strings.value', 'LIKE', '%,'.$v.',%'
->setOr();

//echo Core_DataBase::instance()->getLastQuery();  
}
}
if (CURRENT_STRUCTURE_ID == 580) {
$Informationsystem_Controller_Show->show();
}
else {
$Informationsystem_Controller_Show
->informationsystemItems()
->queryBuilder()
->close()
->clearOrderBy()
->orderBy('RAND()'
->where('property_value_strings.value', '=', '';
}


$Informationsystem_Controller_Show->show();
}
?>
Модератор
#
Re: Как выводить случайные товары API.6?
1. у Вас тут какая-то помесь Shop и Informationsystem, приведите все к одному виду.
2. Фильтрация по свойствам описана http://www.hostcms.ru/documentation/modules/core/controllers/
3. Непонятна в общем задача и о каких ошибках и при каких условиях идет речь.
#
Re: Как выводить случайные товары API.6?
Я пытаюсь привести к одному виду,  сортировка работает, но вот фильтрация по условию не всегда срабатывает (моя задача: нужно выводить элементы ИС только на тех страницах, id которых есть в доп. свойствах данного элемента )
подскажите, что не так делаю, я пока еще новичок, сильно не ругайте.
<?php

     $oInformationsystem = Core_Entity::factory('informationsystem', 87);
    
     // Выбрать всех потомков
    function getChildren($oParentInformationsystemGroup, $aGroups = array())
     {
         $aGroups[] = $oParentInformationsystemGroup->id;
         $aChildren = $oParentInformationsystemGroup->Informationsystem_Groups->getAllByActive(1);
         foreach ($aChildren as $oChildrenGroup)
         {
             $aGroups = getChildren($oChildrenGroup, $aGroups);
         }
         return $aGroups;
     }

$aGroups1 = getChildren(Core_Entity::factory('Informationsystem_Group', 670));
$aGroups2 = getChildren(Core_Entity::factory('Informationsystem_Group', 668));
$aGroupsRes = array_merge((array)$aGroups1, (array)$aGroups2);

$Informationsystem_Controller_Show = new Informationsystem_Controller_Show(
     $oInformationsystem
);

   $Informationsystem_Controller_Show
->xsl(
   Core_Entity::factory('Xsl'->getByName('СписокЭлементовPUBL_second'
   )
->groupsMode('tree'
->group(FALSE)
->limit(2)
       ->itemsProperties(TRUE)
       ->itemsPropertiesList(FALSE);

      if (CURRENT_STRUCTURE_ID != 580) {
         $oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id'
            ->from('properties'
            ->where('properties.tag_name', 'LIKE', 'listIdStructure';
            
          $Informationsystem_Controller_Show
            ->informationsystemItems()
            ->queryBuilder()
            ->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id'
            ->where('property_value_strings.property_id', 'IN', $oCore_QueryBuilder_Select2)
->where('informationsystem_items.informationsystem_group_id', 'IN', $aGroupsRes)
            ->open();
      
         foreach ($structure_parent_id_array as $v) {
             $Informationsystem_Controller_Show
               ->informationsystemItems()
               ->queryBuilder()
               ->where('property_value_strings.value', 'LIKE', '%,'.$v.',%'
               ->setOr();
          }
      }
      if (CURRENT_STRUCTURE_ID == 580) {
       $Informationsystem_Controller_Show
         ->informationsystemItems()
         ->queryBuilder()
         ->clearOrderBy()
         ->orderBy('RAND()'
->where('informationsystem_items.informationsystem_group_id', 'IN', $aGroupsRes);
      }
      else {
       $Informationsystem_Controller_Show
         ->informationsystemItems()
         ->queryBuilder()
         ->where('property_value_strings.value', '=', ''
         ->close()
         ->clearOrderBy()
         ->orderBy('RAND()';
      }

   $Informationsystem_Controller_Show->show();

Когда у меня доступно всего 5 элементов, с нужным доп. свойством - все работает, а когда их много - фильтруется уже не верно.
#
Re: Как выводить случайные товары API.6?
Спасибо.
Получилось. Надо было просто выводить не $v а $structure_parent_id_array[0]

так
foreach ($structure_parent_id_array as $v) {
             $Informationsystem_Controller_Show
               ->informationsystemItems()
               ->queryBuilder()
               ->where('property_value_strings.value', 'LIKE', '%,'.$structure_parent_id_array[0].',%'
               ->setOr();
Авторизация