0

hope you are all good and in great shape.

TLDR: How can I filter my product grid on the admin side using a WEIGHT RANGE?

I am a newbie in Magento and I have been doing some progress over the last months on the basics of customizing Magento to our small company. I had a request from the sales team which I am struggling to get to term. We sell a kind of product which has a different weight to every item and the final price of the product is calculated by multiplying a variable rate by the weight. It makes the weight a very important attribute in the system not only for the sales/logistics, but also for the client's decision making. Therefore, I need to filter the product catalog grid on the admin side by weight, but not by a specific number, instead, I need a RANGE of weight. (From ...kg to ...kg) I managed to create the field, but I am struggling on the part were the action happens. Please take a look on the screenshots below: Weight Filter on the admin side

Weight Range column in the admin side

This is the structure of the module that I created:

Module structure with files

And here is the code on the file AddWeightFieldToCollection.php which I am struggling with:

<?php

namespace Diamwill\WeightFilter\Ui\DataProvider\Product;

use Magento\Framework\Data\Collection;

use Magento\Ui\DataProvider\AddFieldToCollectionInterface;

class AddWeightFieldToCollection implements AddFieldToCollectionInterface

{

    public function addField(Collection $collection, $field, $alias = null)

    {

      $mg_eav_attribute = $collection->getResource()->getTable('mg_eav_attribute');
      $mg_catalog_product_entity_decimal = $collection->getResource()->getTable('mg_catalog_product_entity_decimal');
      $collection->getSelect()->joinLeft(
          ['soa' => $mg_eav_attribute],
          'soa.entity_type_id = 4 AND soa.attribute_code = \'weight\'',
          null
      );
      $collection->getSelect()->joinLeft(
          ['dcrt' => $mg_catalog_product_entity_decimal],
          'soa.attribute_id = dcrt.attribute_id',
          ['code']
      );

        $collection->joinField(

            'weight_range',

            'mg_catalog_product_entity',

            'value',

            'mg_catalog_product_entity.entity_id = soa.entity_id AND soa.store_id = 0',

            null,

            'left'

        );

    }

}

Basically, I know that I have to join the product tables to get weight, which in SQL terms would be something like this:

SELECT 
    w1.value AS 'weight'
FROM
    mg_catalog_product_entity AS prod
        LEFT JOIN
            mg_catalog_product_entity_decimal w1 ON 
                prod.entity_id = w1.entity_id
                AND w1.store_id = 0
                AND w1.attribute_id = (SELECT 
                        attribute_id
                    FROM
                        mg_eav_attribute
                    WHERE
                        attribute_code = 'weight'
                            AND entity_type_id = (SELECT 
                                entity_type_id
                            FROM
                                mg_eav_entity_type
                            WHERE
                                entity_type_code = 'catalog_product'));

Which I am not capable of linking with the Collection class and apply to the column, in order to filter. Can someone help me with the code? I am happy to read a tutorial to learn or to get a certain book/article with more information on the topic.

Please, I do not want to buy a plugin/module in order to do that. I would like to learn how to do it.

Thanks to anyone who can help me.

Have a great day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.