Question

I've requirement like following.

Products price is calculated on the fly and I want price sorting to be proper with it.

I've extend the block Magento\Catalog\Block\Product\ListProduct

Following is code

protected function _getProductCollection()
    {
        $sortingOrder =  $this->getToolbarBlock()->getCurrentOrder();
        $sortingDirection = $this->getToolbarBlock()->getCurrentDirection();
        $collection = parent::_getProductCollection();

        if ($collection != null && $sortingOrder == 'price') {
            $list = array();
            $templist = array();
            foreach ($collection as $k => $product){
                $newPrice = $product->getPrice();
                $list[] = array('sku'=>$product->getSku(),'newprice'=>$newPrice);
            }
            foreach ($list as $v){
                $templist[] = "SELECT {$v['sku']} as newsku, {$v['newprice']} as newprice";
            }
           $newsql = implode(' UNION ', $templist);
            $selectTwoSql =$collection->getSelect()->__toString();

            $collection->getSelect()->reset();
            $collection->getSelect()->union(array(
                new \Zend_Db_Expr($selectTwoSql),
                new \Zend_Db_Expr($newsql),
            ));

            echo $collection->getSelect();
            die;
        }

        return $collection;
    }

In above code I've tried to create columns in collection.

My query o/p is as following

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `stock_status_index`.`stock_status` AS `is_salable`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index_store1` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='38' INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 1 AND stock_status_index.stock_id = 1 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 INNER JOIN `search_tmp_5cf5a6d29872b5_06618402` AS `search_result` ON e.entity_id = search_result.entity_id WHERE ((stock_status_index.stock_status = 1)) AND (e.created_in <= '1552348800') AND (e.updated_in > '1552348800') ORDER BY `price_index`.`min_price` asc, `e`.`entity_id` DESC LIMIT 12 UNION SELECT 10304703 as newsku, 66.0000 as newprice UNION SELECT 10212044 as newsku, 66.0000 as newprice UNION SELECT 10212016 as newsku, 76.0000 as newprice UNION SELECT 10212014 as newsku, 76.0000 as newprice UNION SELECT 10212013 as newsku, 76.0000 as newprice UNION SELECT 10304694 as newsku, 104.0000 as newprice UNION SELECT 10362088 as newsku, 90.0000 as newprice UNION SELECT 10382742 as newsku, 99.0000 as newprice UNION SELECT 10382741 as newsku, 99.0000 as newprice UNION SELECT 10362089 as newsku, 90.0000 as newprice UNION SELECT 10269385 as newsku, 227.0000 as newprice UNION SELECT 10383265 as newsku, 204.0000 as newprice

After executing query in phpmyadmin, I'm not getting newsku and newprice column.

I want the collection to be sorted based on newprice which is created on the fly.

Maybe I'm following wrong way but this price data are not being saved anywhere else so I'm left with no choice but to calculate like this.

Can anyone please guide me?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top