0

I'm working on a Magento 2 project where I need to display custom reports under the order grid on the Sales Orders page. Specifically, I want to show the following fields:

  1. Total (totals sum)
  2. Subtotal (subtotals sum)
  3. Shipping (shipping sum)
  4. VAT (VAT sum)
  5. Quantity (quantities sum)
  6. Number of Orders
  7. Number of Canceled Orders

Initially, I attempted to achieve this using an observer, but I faced difficulties in accessing the filtered orders collection or the underlying query.

Now, I'm considering implementing a solution using a plugin. Could experienced Magento developers provide guidance on how to tackle this problem effectively? Any insights or alternative approaches would be greatly appreciated.

Feel free to ask for further clarification if needed.

1 Answer 1

0

Implementing custom reports under the order grid in Magento 2 can be accomplished using various methods, including observers, plugins, or customizations to the order grid itself. Since you're facing difficulties accessing the filtered orders collection or underlying query with observers, using a plugin might be a better approach.

Here's a step-by-step guide on how you can implement this using a plugin:

1. Identify the Target Class and Method: Determine which class and method you need to intercept to add your custom functionality. In this case, you'll likely want to intercept the method responsible for loading orders on the Sales Orders page and modify the data before it's displayed.

2. Create Your Plugin Class: Create a custom module for your Magento project if you haven't already. Inside your module, create a plugin class that will intercept the target method. Make sure to define the necessary dependencies in your module's di.xml file.

3. Implement Your Plugin Logic: Inside your plugin class, implement the logic to calculate the custom report fields. You may need to retrieve the orders collection, perform calculations, and add the necessary data to be displayed in the order grid.

4. Modify the Order Grid: Finally, modify the order grid template file to display your custom report fields. You can override the default order grid template in your module and add the necessary HTML to display your custom data.

Here's a simplified example of how your plugin class might look:

<?php
namespace Vendor\Module\Plugin;

class OrderGridPlugin
{
    public function afterSetCollection(
        \Magento\Sales\Block\Adminhtml\Order\Grid $subject,
        $result
    ) {
        // Access the orders collection
        $collection = $subject->getCollection();

        // Calculate custom report fields
        $totalsSum = 0;
        $subtotalsSum = 0;
        $shippingSum = 0;
        $vatSum = 0;
        $quantitiesSum = 0;
        $numberOfOrders = $collection->getSize();
        $numberOfCanceledOrders = $collection->addFieldToFilter('status', \Magento\Sales\Model\Order::STATE_CANCELED)->getSize();

        foreach ($collection as $order) {
            $totalsSum += $order->getGrandTotal();
            $subtotalsSum += $order->getSubtotal();
            $shippingSum += $order->getShippingAmount();
            $vatSum += $order->getTaxAmount();
            $quantitiesSum += $order->getTotalQtyOrdered();
        }

        // Add custom report fields to the order grid
        $subject->addColumnAfter(
            'custom_totals_sum',
            [
                'header' => __('Total (totals sum)'),
                'index' => 'custom_totals_sum',
                'type' => 'number',
            ],
            'grand_total'
        );

        // Repeat the same for other custom fields

        // Set custom report data
        $subject->addTotal(
            new \Magento\Framework\DataObject(
                [
                    'code' => 'custom_totals_sum',
                    'value' => $totalsSum,
                    'label' => __('Total (totals sum)'),
                ]
            )
        );

        // Repeat the same for other custom fields

        return $result;
    }
}

Don't forget to replace Vendor\Module with your actual module namespace.

This is a basic example to get you started. Depending on your specific requirements and Magento version, you may need to adjust the implementation accordingly.

Remember to test thoroughly to ensure that your customizations work as expected and don't interfere with the core functionality of Magento.

1
  • Chatgpt gave me the same answer. But the problem is that \Magento\Sales\Block\Adminhtml\Order\Grid is undefined. there is no such namespace. BTW I use magento 2 open source software (just FYI).
    – Nika Kiria
    Commented Apr 30 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.