/   /   /  OroCommerce for developers : add product image to order items in admin order view

Note:

For more extensions and themes visit our store

OroCommerce for developers : add product image to order items in admin order view


Today we will add useful technique , by adding a thumbnail image to items grid inside admin order view , the seller need this feature to detect quickly which item has sold exactly .

You can download: Free Extension For OroCommerce Order Items Image (Clickable) At Admin


Inside this small course we will customize order items grid by Event Listener  , you can refer to official docs https://doc.oroinc.com/backend/entities/customize-datagrids/


Our bundle in this tutorial is OrderItemImageBundle inside src/Ibnab/Bundle , the grid name of order items inside admin side is order-line-items-grid you can find in : vendor/oro/commerce/src/Oro/Bundle/OrderBundle/Resources/config/oro/datagrids.yml , you can use browser inspector or symfony profiler to find the name too.

Let’s start by creating of Event Listener , So add services.yml inside src/Ibnab/Bundle/OrderItemImageBundle/Resources/config/services.yml and load by DI , the content of our service well:

  1.  
  2. services:
  3.     ibnab_order_item_image.event_listener.order_items_datagrid.event_listener:
  4.         class: Ibnab\Bundle\OrderItemImageBundle\EventListener\OrderItemImageEventListener
  5.         tags:
  6.             - { name: kernel.event_listener, event: oro_datagrid.datagrid.build.pre.order-line-items-grid, method: onPreBuild }

So is simple we’re tagging default symfony  kernel.event_listener , toe observe the event build.pre of our datagrid  order-line-items-grid :

  1.  
  2. event: oro_datagrid.datagrid.build.pre.order-line-items-grid

and the method where we well add the code is  onPreBuild inside the new class Ibnab\Bundle\OrderItemImageBundle\EventListener\OrderItemImageEventListener , the code of our class:

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\OrderItemImageBundle\EventListener;
  4. use Oro\Bundle\DataGridBundle\Event\PreBuild;
  5. class OrderItemImageEventListener
  6. {
  7.     /**
  8.      * @param PreBuild $event
  9.      */
  10.     public function onPreBuild(PreBuild $event): void
  11.     {
  12.           $config = $event->getConfig();
  13.           $config->offsetSetByPath('[columns][product]', ['label' => 'oro.product.entity_label','type' => 'twig','frontend_type' => 'html' ,'template' => 'IbnabOrderItemImageBundle:Order:Datagrid/product_image.html.twig']);
  14.     }
  15. }

So with  PreBuild $event instance we’re getting the configuration of datagrid on pre build step:
  1.  
  2. $config = $event->getConf();

by method  offsetSetByPath we have added a custom renderer to column product of order items datagrid :
  1.  
  2. $config->offsetSetByPath('[columns][product]', ['label' => 'oro.product.entity_label','type' => 'twig','frontend_type' => 'html' ,'template' => 'IbnabOrderItemImageBundle:Order:Datagrid/product_image.html.twig']);


let’s add our twig renderer inside src/Ibnab/Bundle/OrderItemImageBundle/Resources/views/Order/Datagrid/product_image.html.twig and the content is :
  1.  
  2. {% set isFreeFormProduct = record.getValue('productName') is empty and record.getValue('freeFormProduct') is not empty %}{% if isFreeFormProduct %}
  3.     {{ record.getValue('freeFormProduct') }}
  4. {% else %}
  5.      {% import 'OroProductBundle::image_macros.html.twig' as Image %}
  6.      {% set productImage = record.getValue('product').imagesByType('listing').first.image|default(null) %}
  7.      {% set productImageUrl = Image.url(productImage, 'product_small') %}
  8.     <a href=" {{ path('oro_product_view',{'id': record.getValue('product').id }) }}" target="_blank">
  9.        <img src="{{ productImageUrl }}"
  10.                                      class="image_inline"
  11.                                      alt="{{ record.getValue('productName')  }}">
  12.       {{ record.getValue('productName')  }}
  13.     </a>
  14. {% endif %}

easy we have got the code of default product column renderer and adding adding bit code to show image product too:

  1.  
  2.      {% import 'OroProductBundle::image_macros.html.twig' as Image %}
  3.      {% set productImage = record.getValue('product').imagesByType('listing').first.image|default(null) %}
  4.      {% set productImageUrl = Image.url(productImage, 'product_small') %}
  5.     <a href=" {{ path('oro_product_view',{'id': record.getValue('product').id }) }}" target="_blank">
  6.        <img src="{{ productImageUrl }}"
  7.                                      class="image_inline"
  8.                                      alt="{{ record.getValue('productName')  }}">
  9.       {{ record.getValue('productName')  }}
  10.     </a>

Is all => some part of the tutorial need to refer to some basic symfony technique.

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

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.