Skip to content

Adding 'is_saleable' attribute to sort of product collection causes exception and adding 'is_salable' has no effect #7768

@dnadle

Description

@dnadle

In Magento\Catalog\Model\ResourceModel\Product\Collection, addAttributeToSort() has the following lines:

        } elseif ($attribute == 'is_saleable') {
            $this->getSelect()->order("is_saleable " . $dir);
            return $this;
        }

But the generated SQL query selects stock_status_index.stock_status as 'is_salable', not 'is_saleable'. So adding 'is_saleable' causes an SQL exception and since 'is_salable' is not a product attribute, it silently fails to be added to the SQL statement.

Preconditions

  1. Magento CE 2.1.2

Steps to reproduce

  1. Modify a collection prior to load with $collection->addAttributeToSort("is_saleable").

Expected result

  1. The product stock_status is included in the query sort clause.

Actual result

  1. Exceptions:
2 exception(s):
Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_saleable' in 'order clause', query was: SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `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`, `stock_status_index`.`stock_status` AS `is_salable`, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_category_product_index` 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='245'
 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
 LEFT JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 0 AND stock_status_index.stock_id = 1
 INNER JOIN `search_tmp_584f50b9a23f51_22855701` AS `search_result` ON e.entity_id = search_result.entity_id
 LEFT JOIN `catalog_product_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_name_default`.`attribute_id` = '70') AND `at_name_default`.`store_id` = 0
 LEFT JOIN `catalog_product_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '70') AND (`at_name`.`store_id` = 1) ORDER BY `is_saleable` DESC, `name` ASC
 LIMIT 32
Exception #1 (PDOException): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_saleable' in 'order clause'

Workaround

Modify the collection instead with $collection->getSelect()->order('is_salable');

Metadata

Metadata

Assignees

Labels

Component: CatalogFixed in 2.2.xThe issue has been fixed in 2.2 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releasebug report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions