Magento - addFieldToFilter没有选择合适的产品

时间:2013-07-20 10:32:27

标签: php mysql magento magento-1.7

我目前正在为Magento编写产品配置模块。到目前为止,除了核心功能之外,一切都运行良好:从数据库中为所选属性选择正确的产品。我的属性属于一个属性组,从中生成我的配置器的表单字段,我使用的值是管理字段。商店语言是瑞典语。

该商店包含一个可配置产品,其中包含与之相关的简单产品。简单的产品不会在商店中单独展示。现在不是我的属性的所有组合都存在,但它至少应该为我知道存在的一个组合返回正确的产品。首先它总是返回null,现在它返回所有产品。如何选择产品才能实现?

首先,在我激活“在产品列表中使用”之前,属性甚至没有显示在查询中。

提前感谢您的任何帮助:)

这是我的getProduct方法:

public function getProduct($attributes)
{

    Mage::Log($attributes);

    //Get Product Collection
    $collection = Mage::getModel('catalog/product')->getCollection();

    $collectionCount = count($collection);

    Mage::Log($collectionCount);

    //Filter for Selected Product
    $collection->addFieldToFilter('doorconfig_enable',array('eq' => 'Yes'));

    foreach ($attributes as $key => $value) 
    { 
        $collection->addFieldToFilter($key,array('eq' => $value));
    }

    $selection = $collection->getSelect()->__toString();

    Mage::Log($selection);

    $collectionCount = count($collection);

    Mage::Log($collectionCount);

    $product = $collection->getFirstItem();

    return $product;

}

$ attributes参数包含正确提交的POST数据:

[doorconfig_color] => 000000
[doorconfig_type] => lines
[doorconfig_size] => 2500x1800
[doorconfig_remote] => no
[doorconfig_digitalkeypad] => No
[doorconfig_extraremotecontrol] => No
[configdoor_addemergencylock] => No
[doorconfig_insideopeningbutton] => No
[doorconfig_window] => No window

“configdoor_addemergencylock”是我亲爱的同事的拼写错误,但仍然可以从数据库中正确读取(只要有人想知道)。由于我不擅长SQL并且在Magento方面经验不足,我不知道我的查询是对还是错:

2013-07-20T09:47:19+00:00 DEBUG (7): SELECT 1 AS `status`, `e`.`entity_id`, 
`e`.`type_id`, `e`.`attribute_set_id`, `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`, `e`.`doorconfig_color`, `e`.`doorconfig_color_value`, 
`e`.`doorconfig_type`, `e`.`doorconfig_type_value`, `e`.`doorconfig_size`, 
`e`.`doorconfig_size_value`, `e`.`doorconfig_remote`, `e`.`doorconfig_remote_value`, 
`e`.`doorconfig_digitalkeypad`, `e`.`doorconfig_digitalkeypad_value`, 
`e`.`doorconfig_extraremotecontrol`, `e`.`doorconfig_extraremotecontrol_value`, 
`e`.`configdoor_addemergencylock`, `e`.`configdoor_addemergencylock_value`, 
`e`.`doorconfig_insideopeningbutton`, `e`.`doorconfig_insideopeningbutton_value`, 
`e`.`doorconfig_window`, `e`.`doorconfig_window_value` FROM `catalog_product_flat_1` 
AS `e` 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 WHERE (e.doorconfig_color = '000000') 
AND (e.doorconfig_type = 'lines') AND (e.doorconfig_size = '2500x1800') 
AND (e.doorconfig_remote = 'no') AND (e.doorconfig_digitalkeypad = 'No') 
AND (e.doorconfig_extraremotecontrol = 'No') AND (e.configdoor_addemergencylock = 'No') 
AND (e.doorconfig_insideopeningbutton = 'No') AND (e.doorconfig_window = 'No window')

2 个答案:

答案 0 :(得分:0)

您可以尝试过滤产品系列

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(*);
$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 'Yes'));

答案 1 :(得分:0)

什么类型的“doorconfig_enable”属性?如果是布尔值,请使用“0”或“1”。如果“doorconfig_enable”是select属性,则需要使用选项id。 所以:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('doorconfig_enable')
    ->addAttributeToFilter('doorconfig_enable',array('eq' => 1));

Mage::log($collection->getSize(),null,'custom.log');

如果您需要获取选项ID,请结帐this