Magento 1.9 AddAttributetoFilter:文字列の代わりに数値を比較
-
13-12-2019 - |
質問
マゼントバックエンドでは、独自の製品属性「高さ」を作成しました。入力タイプは "text"です( "float"のような類似型はありませんでした)。この属性をフィルタとして使用するには、小さい拡張子を書きました。この拡張では、
で製品を絞り込む$this->_productCollection->addAttributeToFilter('height', array('lteq' => $height) );
.
製品の高さが69cm(69.00)で、フィルタは "<= 80cm"(LTEQ 80.00)の場合、フィルタはうまく機能します。しかし、フィルタが "<= 100cm"のときはうまくいきません(LTEQ 100.00)。これは、MagentoがVARCHARとしてheight-属性を作成したため、関数addAttributOfilterはstring-compareを作成します。 「6」が「1」より大きいため、「69」が「100」より大きい。
(私は最善の方法が属性を削除し、拡張子を介してタイプ "float"を持つ新しいものを作成することを知っています。しかし、店はほぼ完成しており、すでに製品が含まれているので、そのような「大きい」変更を加えるのを避けたいと思いますこの状態で。)
解決
TextPart
のプリティークのコメントPHP
ではなくMySQL比較になる予定です。
フィールドタイプを変更せずに解決策に導きます。関数 _getProductCollection に mage_catalog_block_product_list のコードを変更しました。
...
if(isset($_GET['height']) && ''!=trim($_GET['height'])){
// this line adds the attribute "height" to the query (without this line we couln't use the field in our own constraint)
$this->_productCollection->addAttributeToFilter('height', array('gteq' => 0) );
// we are in search (in search there is an additional height-table with the name at_height_default)
if(false!==strpos($this->_productCollection->getSelect(), 'at_height_default')){
$this->_productCollection->getSelect()->where(new Zend_Db_Expr(
"CAST(IF(at_height.value_id > 0, at_height.value, at_height_default.value) AS DECIMAL(9,2)) <= ".(float) $height
));
}
// we are in normal list-view (here the height-table has the name at_height)
else{
$this->_productCollection->getSelect()->where(new Zend_Db_Expr(
"CAST(at_height.value AS DECIMAL(9,2)) <= ".(float) $height
));
}
}
...
.
もちろん、コア内のコードを変更しませんでした。私は私の拡張子の関数を上書きしました。タイプキャストのために、値は文字列の代わりに数値として比較されます。
それは最もきれいな解決策ではなく、現時点で私のために大丈夫です。 所属していません magento.stackexchange