Jump to content

Recommended Posts

I am currently working on a project where I need to add an additional column to the products block in the orders view page. Despite extensive research, I have not been able to find a suitable solution. My approach involves creating a module that extends the OrderController class and overrides the public function getProductsListAction(int $orderId): Response(). However, I am unsure if this is the correct way to achieve my goal. I would appreciate any guidance or suggestions. Below is my current code and folder structure:

 

modules/
├── customordermodule/
│   ├── controllers/
│   │   └── admin/
│   │       └── AdminMyOrderPageController.php
│   ├── views/
│   │   └── templates/
│   │       └── admin/
│   │           └── order/
│   │               └── product_list.tpl
│   ├── customordermodule.php

 

<?php

 

class AdminMyOrderPageController extends OrderController

{

    public function getProductsListAction(int $orderId): Response

    {

        /** @var OrderForViewing $orderForViewing */

        $orderForViewing = $this->getQueryBus()->handle(new GetOrderForViewing($orderId, QuerySorting::DESC));

        $orderForViewing->swag = 'testing';

        // echo $order_invoice_list;

        // echo "SWAG";

 

        $currencyDataProvider = $this->container->get('prestashop.adapter.data_provider.currency');

        $orderCurrency = $currencyDataProvider->getCurrencyById($orderForViewing->getCurrencyId());

 

        $formBuilder = $this->get('prestashop.core.form.identifiable_object.builder.cancel_product_form_builder');

        $cancelProductForm = $formBuilder->getFormFor($orderId);

 

        $paginationNum = $this->configuration->getInt('PS_ORDER_PRODUCTS_NB_PER_PAGE', self::DEFAULT_PRODUCTS_NUMBER);

        $paginationNumOptions = self::PRODUCTS_PAGINATION_OPTIONS;

        if (!in_array($paginationNum, $paginationNumOptions)) {

            $paginationNumOptions[] = $paginationNum;

        }

        sort($paginationNumOptions);

 

        $isColumnLocationDisplayed = false;

        $isColumnRefundedDisplayed = false;

 

        foreach (array_slice($orderForViewing->getProducts()->getProducts(), $paginationNum) as $product) {

            if (!empty($product->getLocation())) {

                $isColumnLocationDisplayed = true;

            }

            if ($product->getQuantityRefunded() > 0) {

                $isColumnRefundedDisplayed = true;

            }

        }

 

        return $this->render('@PrestaShop/Admin/Sell/Order/Order/Blocks/View/product_list.html.twig', [

            'orderForViewing' => $orderForViewing,

            'cancelProductForm' => $cancelProductForm->createView(),

            'orderCurrency' => $orderCurrency,

            'paginationNum' => $paginationNum,

            'isColumnLocationDisplayed' => $isColumnLocationDisplayed,

            'isColumnRefundedDisplayed' => $isColumnRefundedDisplayed,

            'isAvailableQuantityDisplayed' => $this->configuration->getBoolean('PS_STOCK_MANAGEMENT'),

        ]);

    }

}

 

Any feedback is welcomed!

 

image.png

Link to comment
Share on other sites

@dnk.hack Thank you very much for the comment!  I am able to Dump orderForViewing and OrderProductForViewing from the page and modify the twig file a little bit to print some variables inside! However, I now need to display additional information, specifically 'Base Price' and 'Unit Price,' which are not part of the 'OrderForViewing' and 'OrderProductForViewing' objects. I want to somehow retrieve information named 'Base Price' and 'Unit Price' in the invoice pdf and display it as an additional column. My research suggests that the only way to achieve this is by using a decorator. Is this understanding correct?

This is someof the work that I did to use a decorator. 

 

<?php

namespace MyModule\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use PrestaShopBundle\Controller\Admin\ShopParameters\OrderController;  // Import the original controller you want to decorate

 

class AdminMyOrderPageController extends AbstractController

{

    private $decoratedController;

    public function __construct(OrderController $decoratedController)

    {

        $this->decoratedController = $decoratedController;

    }

 

    public function getProductsListAction(int $orderId): Response

    {

        // Manipulate the output

        $output = $this->decoratedController->getProductsListAction($orderId);

        // Modify the $output as needed to retrieve pdf data.

        $outputData = $output->getData();

        $outputData['testing'] = 'testing22';

        $output->setData($outputData);

        return $output;

    }

}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...