Jump to content

How to add Product Reference to search results on back-end create order page?


Matt E

Recommended Posts

I would like to add the product reference before the name of each product when creating new order on back-end where the arrow is in this screenshot...

product-search.jpg.63836e60931ff4453b4a50f342c5a168.jpg

I have found searchProductsAction function in "PrestaShopBundle\Controller\Admin\Sell\Order\OrderController" which uses SearchProducts and FoundProduct controllers located here:

use PrestaShop\PrestaShop\Core\Domain\Product\Query\SearchProducts;
use PrestaShop\PrestaShop\Core\Domain\Product\QueryResult\FoundProduct;

Also, I see these results are assembled by the admin\themes\new-theme\public\order_create.bundle.js script using the $name attribute of the $foundProducts array from an AJAX call.

As an experiment, I was able to change what was displayed in the results list by modifying the $name variable in the FoundProduct.php core controller, like this:

product-search2.jpg.1a18d51354fd9b8457232c914897af35.jpg

However this file does not declare the $reference variable like it does in the nearby ProductDetails.php controller. In fact, trying to declare and return any other variable in FoundProduct.php results in a 500 error when performing the search function.

I was able to succsesfully remap the admin_orders_products_search route to a custom controller to override the searchProductsAction function, but I am stuck on how to correctly modify the $name attribute to include the $reference for each $foundProduct before returning it to the order_create.bundle.js script to display the results, and do so using an override instead of actually editing the core controller file.

Can someone more experienced please advise the correct procedure to achieve this?

Edited by Matt E (see edit history)
Link to comment
Share on other sites

Three files need to be edited.

1. AdminDir/themes/new-theme/public/order_create.bundle.js

unminify this JavaScript here: https://unminify.com/   

find:

key: "renderFoundProducts",

    change to:

key: "renderFoundProducts",
	value: function (e) {
		(0, o.default)(e).forEach(function (e) {
			var t = e.name;
			var tr = e.reference;
			0 === e.combinations.length && (t += " - " + e.formattedPrice), c(a.default.productSelect).append('<option value="' + e.productId + '">' + tr+ ': '+t + "</option>");
		});
	},

 

 

2. src/Core/Domain/Product/QueryResult/FoundProduct.php

    add reference variable:

obrazek.png.3f0e78d4d89f001e9595c47a00713af8.png

  

    change __construct to:

public function __construct(
        int $productId,
        string $reference,
        string $name,
        string $formattedPrice,
        float $priceTaxIncl,
        float $priceTaxExcl,
        float $taxRate,
        int $stock,
        string $location,
        bool $availableOutOfStock,
        array $combinations = [],
        array $customizationFields = []
    ) {
        $this->productId = $productId;
        $this->reference = $reference;
        $this->name = $name;
        $this->formattedPrice = $formattedPrice;
        $this->priceTaxIncl = $priceTaxIncl;
        $this->priceTaxExcl = $priceTaxExcl;
        $this->taxRate = $taxRate;
        $this->stock = $stock;
        $this->location = $location;
        $this->availableOutOfStock = $availableOutOfStock;
        $this->combinations = $combinations;
        $this->customizationFields = $customizationFields;
    }

 

    add new function after getProductId():

    /**
     * @return string
     */
    public function getReference(): string
    {
        return $this->reference;
    }

 

 

3. src/Adapter/Product/QueryHandler/SearchProductsHandler.php

    add reference to FoundProduct:

obrazek.thumb.png.96d2b9a6de1f4660780ce13b79abb290.png

 

4. Come back here and give me a Like by clicking the gray heart below my posts 😄

Edited by 4you.software (see edit history)
Link to comment
Share on other sites

On 10/9/2022 at 12:54 AM, 4you.software said:

Three files need to be edited.

Thanks for the reply, it was helpful to make some progress, but unfortunately I need to do this without overriding the core files.

I am trying to override or decorate the SearchProducts query handler (prestashop.adapter.product.query_handler.search_products) or class (PrestaShop\PrestaShop\Adapter\Product\QueryHandler\SearchProductsHandler) using a custom module...

I already defined the class/namespace with composer.json and routes.yml. I know this is working because I successfully override the OrderController.php to my custom version.

Next I created modules\MyModule\config\Services.yml:

services:
  _defaults:
    public: true

  prestashop.adapter.product.query_handler.search_products:
    class: 'MyModule\Controller'

Then I copied SearchProductsHandler.php from src\Adapter\Product\QueryHandler to modules\MyModule\src\Controller change the namespace to MyModule\Controller and made some small changes to the functions to test if it's working.

Now when I try to do the search on the create order page, console logs 500 error with this message:

[MissingHandlerException code 0]: Missing handler for command PrestaShop\\PrestaShop\\Core\\Domain\\Product\\Query\\SearchProducts

This is confusing because when I run terminal command "php bin/console debug:container search_products" I get this response:

Information for Service "prestashop.adapter.product.query_handler.search_products"
==================================================================================

 ---------------- ----------------------------------------------------------
  Option           Value
 ---------------- ----------------------------------------------------------
  Service ID       prestashop.adapter.product.query_handler.search_products
  Class            MyModule\Controller
  Tags             -
  Public           yes
  Synthetic        no
  Lazy             no
  Shared           yes
  Abstract         no
  Autowired        no
  Autoconfigured   no
 ---------------- ----------------------------------------------------------

Can someone explain why the 500 error when the SearchProducts handler service seems correctly re-mapped to the module?

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...