我在magento-sectivend中创建了自己的产品属性“高度”。输入类型是“文本”(因为没有类似类型的类型,如“float”)。要将此属性用作过滤器,我写了一个小的扩展名。在此扩展中,我用

过滤产品
$this->_productCollection->addAttributeToFilter('height', array('lteq' => $height) );
.

当产品高度为69厘米(69.00)并且过滤器是“<= 80cm”(LTEQ 80.00),过滤器工作正常。但是,当过滤器是“<= 100cm”(LTEQ 100.00)时,它不起作用。这是因为Magento创建了varchar的高度属性,因此函数addattributofilter会进行字符串比较。在字符串 - 比较“69”中大于“100”,因为“6”大于“1”。

有没有办法告诉addattributetofilter,它应该将值与数字进行比较而不是字符串?

(我知道最好的方法是删除该属性,并通过扩展创建一个新的“float”的新一个。但该商店几乎完了,已经包含产品,所以我想避免制作这样的“大”的变化在这个状态。)

有帮助吗?

解决方案

与textpart的prateek评论

这将是mysql比较而不是php

将我引导到解决方案而不改变现场类型。我已将代码更改为 MAGE_CATALOG_BLOCK_PRODUCT_LIST 中的代码 _getProductCollection 中:

...
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
        ));
    }
}
...
.

当然我没有在核心中更改代码。我在扩展名中覆盖了该函数。由于键盘将值与数字进行比较而不是字符串进行比较。 这不是清洁的解决方案,但目前对我来说没问题。

许可以下: CC-BY-SA归因
scroll top