Several years ago I wrote tutorial about: Product manufacturer on back office order page that step by step explains how to display manufacturer (brand) near each product on list of purchased products on order details page in shop's back office. Tutorial was dedicated for PrestaShop 1.6 only. Today i want to explain the same thing, but for PrestaShop 1.7.
How to add brand information to list of purchased products?
Things since 2014 changed a lot. PrestaShop has very flexible core code now it gives much more possibilities than before. But it is also a little more complicated. To apply changes we will have to alter core, not only .tpl file as it was in the past. What is the idea?
Firstly - let's create a function that will return brand name
in Product object class file file located here: classes/Product.php add a new function that will get product's manufacturer name. The code of function is attached below. It has parameter $id_product - as a result function will return name of manufacturer of product with id equal to $id_product parameter value.
public static function getProductManufacturerName($id_product) { $product = new Product($id_product, false, Context::getContext()->language->id); $manufacturer = new Manufacturer($product->id_manufacturer, Context::getContext()->language->id); return $manufacturer->name; }
Secondly - let's add Product Class to twig template file
In PrestaShop 1.7.x branch smarty (.tpl) is continuously changed to twig template engine. Almost each new release of PrestaShop 1.7.x brings new twig files. We need to implement Product class instance into twig template. To achieve this we need to alter file src/prestashopBundle/Controller/Admin/Sell/Order/OrderController.php. At the begining of this file you can find code like:
namespace PrestaShopBundle\Controller\Admin\Sell\Order
right after it add code:
use Product;
Then, in the same file find code:
return $this->render('@PrestaShop/Admin/Sell/Order/Order/view.html.twig', [ 'showContentHeader' => true, 'enableSidebar' => true, 'orderCurrency' => $orderCurrency, 'meta_title' => $metatitle, 'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')), 'orderForViewing' => $orderForViewing, 'addOrderCartRuleForm' => $addOrderCartRuleForm->createView(), 'updateOrderStatusForm' => $updateOrderStatusForm->createView(), 'updateOrderStatusActionBarForm' => $updateOrderStatusActionBarForm->createView(), 'addOrderPaymentForm' => $addOrderPaymentForm->createView(), 'changeOrderCurrencyForm' => $changeOrderCurrencyForm->createView(), 'privateNoteForm' => $privateNoteForm ? $privateNoteForm->createView() : null, 'updateOrderShippingForm' => $updateOrderShippingForm->createView(), 'cancelProductForm' => $cancelProductForm->createView(), 'invoiceManagementIsEnabled' => $orderForViewing->isInvoiceManagementIsEnabled(), 'changeOrderAddressForm' => $changeOrderAddressForm ? $changeOrderAddressForm->createView() : null, 'orderMessageForm' => $orderMessageForm->createView(), 'addProductRowForm' => $addProductRowForm->createView(), 'editProductRowForm' => $editProductRowForm->createView(), 'backOfficeOrderButtons' => $backOfficeOrderButtons, 'merchandiseReturnEnabled' => $merchandiseReturnEnabled, 'priceSpecification' => $this->getContextLocale()->getPriceSpecification($orderCurrency->iso_code)->toArray(), 'previousOrderId' => $orderSiblingProvider->getPreviousOrderId($orderId), 'nextOrderId' => $orderSiblingProvider->getNextOrderId($orderId), 'paginationNum' => $paginationNum, 'paginationNumOptions' => $paginationNumOptions, 'isAvailableQuantityDisplayed' => $this->configuration->getBoolean('PS_STOCK_MANAGEMENT'), 'internalNoteForm' => $internalNoteForm->createView(), ]);
and at the begining of this array add new variable ProductClass, like i show below:
$product = new Product(); return $this->render('@PrestaShop/Admin/Sell/Order/Order/view.html.twig', [ 'ProductClass' => $product, 'showContentHeader' => true, 'enableSidebar' => true, 'orderCurrency' => $orderCurrency, 'meta_title' => $metatitle, 'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')), 'orderForViewing' => $orderForViewing, 'addOrderCartRuleForm' => $addOrderCartRuleForm->createView(), 'updateOrderStatusForm' => $updateOrderStatusForm->createView(), 'updateOrderStatusActionBarForm' => $updateOrderStatusActionBarForm->createView(), 'addOrderPaymentForm' => $addOrderPaymentForm->createView(), 'changeOrderCurrencyForm' => $changeOrderCurrencyForm->createView(), 'privateNoteForm' => $privateNoteForm ? $privateNoteForm->createView() : null, 'updateOrderShippingForm' => $updateOrderShippingForm->createView(), 'cancelProductForm' => $cancelProductForm->createView(), 'invoiceManagementIsEnabled' => $orderForViewing->isInvoiceManagementIsEnabled(), 'changeOrderAddressForm' => $changeOrderAddressForm ? $changeOrderAddressForm->createView() : null, 'orderMessageForm' => $orderMessageForm->createView(), 'addProductRowForm' => $addProductRowForm->createView(), 'editProductRowForm' => $editProductRowForm->createView(), 'backOfficeOrderButtons' => $backOfficeOrderButtons, 'merchandiseReturnEnabled' => $merchandiseReturnEnabled, 'priceSpecification' => $this->getContextLocale()->getPriceSpecification($orderCurrency->iso_code)->toArray(), 'previousOrderId' => $orderSiblingProvider->getPreviousOrderId($orderId), 'nextOrderId' => $orderSiblingProvider->getNextOrderId($orderId), 'paginationNum' => $paginationNum, 'paginationNumOptions' => $paginationNumOptions, 'isAvailableQuantityDisplayed' => $this->configuration->getBoolean('PS_STOCK_MANAGEMENT'), 'internalNoteForm' => $internalNoteForm->createView(), ]);
And lastly - let's run getProductManufacturerName() function in twig file
Twig template file that is responsible for list of purchased products on order details page is located in this path: /src/PrestaShopBundle/Resources/Views/Admin/Sell/Order/Order/Blocks/View/product.html.twig.
List of purchased products appears as a table with columns and rows. Table has heading, so we have to add "manufacturer" to table header that will point column with manufacturer (brand) name.
Heading
Search for code:
<th> <p class="mb-0">{{ 'Total'|trans({}, 'Admin.Global') }}</p> <small class="text-muted">{{ orderForViewing.taxMethod }}</small> </th>
add after:
<th> <p class="mb-0">{{ 'Manufacturer'|trans({}, 'Admin.Global') }}</p> </th>
Fill out manufacturer column with manufacturer (brand) name
Search for code:
<td class="cellProductTotalPrice">{{ product.totalPrice }}</td>
add after:
<td>{{ProductClass.getProductManufacturerName(product.id)}}</td>