Custom module with custom product list
-
12-12-2019 - |
質問
I'm trying to create a new page that lists a custom selection of products (basically a list of SKUs returned from an external service) that uses the default magento layered navigation.
I've been using this as a guideline: http://www.techytalk.info/adding-layered-navigation-custom-controller-action-magento/
But I've hitten a little snag when I'm trying to filter the products, in my:
app/code/local/xyzname/abcname/Model/Catalog/Layer.php
public function prepareProductCollection($collection) {
parent::prepareProductCollection($collection);
$collection
->addAttributeToFilter( 'sku', array( 'in' => $this->skus ) );
//echo $collection->getSelectSql(true);die;
return $this;
}
I'm getting this error:
... Column not found: 1054 Unknown column 'e.sku' in 'where clause', query was: ...
I've read that I could use ->getSelect()->join... to compensate for that missing sku, but databases/sql is something I'm very, very noob about.
Right now the $skus is just an static array.
解決
The following code should work better (in some cases e
denotes catalog_product_index_price
table which doesn't have sku column):
$condition = count($this->skus)
? "sku_filter.sku IN ('" . implode("', '", $this->skus) . "')"
: "sku_filter.entity_id IS NULL";
$collection->getSelect()
->join(
array('sku_filter' => Mage::getSingleton('core/resource')->getTableName('catalog/product')),
"sku_filter.entity_id = e.entity_id AND $condition",
null
);