/   /   /  OroCommerce for developer: how image product list get showed + overview about website indexation

Note:

For more extensions and themes visit our store

OroCommerce for developer: how image product list get showed + overview about website indexation


As all know every big system has a way to index the catalog on flat tables , not logic to make query for example directly on oro_proudct because they has many complex relation with other tables , So to get fast frontend display with best performance using a simple tables as source of data . 
OroCommerce  System has a website indexer tasks which you can done with oro command :
bin/console oro:website-search:reindex

Your catalog data will pushed in some flat tables is:
oro_website
oro_website_search_datetime
oro_website_search_decimal
oro_website_search_integer
oro_website_search_item
oro_website_search_text

For example oro_website_search_text stock data of type text or string …

Let’s start explore how the image get showed in frontend product list page firstly we shuld know who’s the datagird responsible , go to vendor/oro/commerce/src/Oro/Bundle/ProductBundle/Resources/config/oro/datagrids.yml , So search for frontend-product-search-grid :

  1.  
  2.     frontend-product-search-grid:
  3.         options:
  4.             entityHint: oro.product.entity_plural_label
  5.             displayOptions:
  6.                 selector: '.catalog__filter-controls__item_display-options'
  7.             jsmodules:
  8.                 - oroproduct/js/app/datagrid/frontend-product-display-options-builder
  9.                 - oroproduct/js/app/datagrid/frontend-product-filters-events-dispatcher-builder
  10.             frontend: true
  11.         source:
  12.             type: search
  13.             search_index: website
  14.             query:
  15.                 select:
  16.                     - text.sku as sku
  17.                     - integer.newArrival as newArrival
  18.                     - text.names_LOCALIZATION_ID as name
  19.                     - text.shortDescriptions_LOCALIZATION_ID as shortDescription
  20.                     - text.type as type
  21.                     - text.image_product_medium as image_product_medium
  22.                     - text.image_product_large as image_product_large
  23.                     - text.product_units as product_units
  24.                     - text.primary_unit as unit
  25.                 from:
  26.                     - oro_product_WEBSITE_ID
  27.  

You can see that the query select the collection from flat tables but the more important fields :

  1.  
  2.                     - text.image_product_medium as image_product_medium
  3.                     - text.image_product_large as image_product_large
  4.  

We select  image_product_medium and  image_product_large but why ? And how it’s  get pushed to array represent the product So shoud explore the event listener vendor/oro/commerce/src/Oro/Bundle/ProductBundle/EventListener/FrontendProductDatagridListener.php  

and see the function  onResultAfter :

  1.  
  2.     public function onResultAfter(SearchResultAfter $event)
  3.     {
  4.         /** @var ResultRecord[] $records */
  5.         $records = $event->getRecords();
  6.         $this->addProductUnits($records);
  7.         $this->addProductImages($event, $records);
  8.     }
  9.  

You should know datagrid offering many event pre Build ,after build  and after result ….

  1.  
  2.   - { name: kernel.event_listener, event: oro_datagrid.search_datasource.result.after.frontend-product-search-grid, method: onResultAfter }
  3.  

result.after : after building the datagrid and get collection from db you have the ability to make some change like change some data of datagrid records.

So let’s explore the function addProductImages($event, $records) :

  1.  
  2.     protected function addProductImages(SearchResultAfter $event, array $records)
  3.     {
  4.         $gridName = $event->getDatagrid()->getName();
  5.         $theme = $this->themeHelper->getTheme($gridName);
  6.         switch ($theme) {
  7.             case DataGridThemeHelper::VIEW_LIST:
  8.                 $imageFilter = self::PRODUCT_IMAGE_FILTER_MEDIUM;
  9.                 break;
  10.             case DataGridThemeHelper::VIEW_GRID:
  11.                 $imageFilter = self::PRODUCT_IMAGE_FILTER_LARGE;
  12.                 break;
  13.             case DataGridThemeHelper::VIEW_TILES:
  14.                 $imageFilter = self::PRODUCT_IMAGE_FILTER_MEDIUM;
  15.                 break;
  16.             default:
  17.                 return;
  18.         }
  19.         $noImagePath = $this->imagePlaceholderProvider->getPath($imageFilter);
  20.         foreach ($records as $record) {
  21.             $productImageUrl = $record->getValue('image_' . $imageFilter) ?: $noImagePath;
  22.             $record->addData(['image' => $productImageUrl]);
  23.         }
  24.     }
  25.  

So related the row view if grid or list add data to records large or medium:

  1.  
  2.         foreach ($records as $record) {
  3.             $productImageUrl = $record->getValue('image_' . $imageFilter) ?: $noImagePath;
  4.             $record->addData(['image' => $productImageUrl]);
  5.         }
  6.  

Watch video for more infos.
 

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.