php - Magento - addFieldToFilter 没有选择正确的产品

标签 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')

最佳答案

您可以试试这个来过滤您的产品系列

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

关于php - Magento - addFieldToFilter 没有选择正确的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17761302/

相关文章:

php - 带密码的 SHA1,不涉及密码

mysql - 选择不同的 column_a 并按 column_b 分组

mysql - Moodle数据库的多个表之间的条件连接

MySQL AUTO_INCREMENT 不会在任何 INSERT 查询上增加

当我进入管理员时,Magento 货币符号 404

php - magento 类别未显示在添加产品页面上?

php - 多次上传和调整 class.upload.php

php - .htaccess 规则不起作用

Magento:如何添加二级菜单?

php - 在 PHP 脚本中插入 MySQL 数据库之前检查值是否存在