3

I need to override

protected function _productLimitationPrice($joinLeft = false) in magento 2 which is located at
public_html/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php
How can i override method in product collection.

class Collection extends 
           \Magento\Catalog\Model\ResourceModel\Product\Collection

       {
      protected function _productLimitationPrice($joinLeft = false)
   {
      $filters = $this->_productLimitationFilters;
    if (!$filters->isUsingPriceIndex() ||
        !isset($filters['website_id']) ||
        (string)$filters['website_id'] === '' ||
        !isset($filters['customer_group_id']) ||
        (string)$filters['customer_group_id'] === ''
    ) {
        return $this;
    }

    // Preventing overriding price loaded from EAV because we want to use the one from index
    $this->removeAttributeToSelect('price');

    $connection = $this->getConnection();
    $select = $this->getSelect();
    $joinCond = join(
        ' AND ',
        [
            'price_index.entity_id = e.entity_id',
            $connection->quoteInto('price_index.website_id = ?', $filters['website_id']),
            $connection->quoteInto('price_index.customer_group_id = ?', $filters['customer_group_id'])
        ]
    );

    $fromPart = $select->getPart(\Magento\Framework\DB\Select::FROM);
    if (!isset($fromPart['price_index'])) {
        $least = $connection->getLeastSql(['price_index.min_price', 'price_index.tier_price']);
        $minimalExpr = $connection->getCheckSql(
            'price_index.tier_price IS NOT NULL',
            $least,
            'price_index.min_price'
        );
        $colls = [
            'price',
            'tax_class_id',
            'final_price',
            'minimal_price' => $minimalExpr,
            'min_price',
            'max_price',
            'tier_price',
        ];

        $tableName = [
            'price_index' => $this->priceTableResolver->resolve(
                'catalog_product_index_price',
                [
                    $this->dimensionFactory->create(
                        CustomerGroupDimensionProvider::DIMENSION_NAME,
                        (string)$filters['customer_group_id']
                    ),
                    $this->dimensionFactory->create(
                        WebsiteDimensionProvider::DIMENSION_NAME,
                        (string)$filters['website_id']
                    )
                ]
            )
        ];

        if ($joinLeft) {
            $select->joinLeft($tableName, $joinCond, $colls);
        } else {
            $select->joinLeft($tableName, $joinCond, $colls);
        }
        // Set additional field filters
        foreach ($this->_priceDataFieldFilters as $filterData) {
            $select->where(call_user_func_array('sprintf', $filterData));
        }
    } else {
        $fromPart['price_index']['joinCondition'] = $joinCond;
        $select->setPart(\Magento\Framework\DB\Select::FROM, $fromPart);
    }
    //Clean duplicated fields
    $this->_resourceHelper->prepareColumnsList($select);

    return $this;

  }
5
  • what are you trying to do with _productLimitationPrice method?
    – fmsthird
    Commented May 14, 2019 at 11:10
  • i need to changed the product collection to display grouped products with out simple products associated.in magento 2.2.5 , if we add a grouped products there is an entries added in catalog_product_index_price with price values as null , when we read product collection there are using inner join with catalog_product_index_price table , so that the grouped products without simple products are displaying in list pages.
    – Prasanth
    Commented May 14, 2019 at 11:18
  • But in magento 2.2.6 when we create a grouped product there are no entries in catalog_product_index_price table ,when an simple product is assigned then entries are entered in database. Due to this grouped products without simple are not displaying in frontend. We need to change the collection that instead of inner join we use left join to get the products in list pages.
    – Prasanth
    Commented May 14, 2019 at 11:22
  • did you run di:compile?
    – fmsthird
    Commented May 14, 2019 at 11:41
  • yes i ran di:comile
    – Prasanth
    Commented May 14, 2019 at 12:14

1 Answer 1

1

_productLimitationPrice method is protected so you can not able to make a plugin.

So you have to override by preference method

public_html/app/code/vendor/module/etc/di.xml

add below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="Vendor\Module\Model\ResourceModel\Product\Collection"/>
</config>

Vendor\Module\Model\ResourceModel\Product\Collection.php

_productLimitationPrice(){

    make your custom code
}
3
  • But it is not working
    – Prasanth
    Commented May 14, 2019 at 8:06
  • @Prasanth Can you please add your Collection.php code in question Commented May 14, 2019 at 8:21
  • Thanks for helping. I added collection in question.
    – Prasanth
    Commented May 14, 2019 at 10:43

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.