1

我目前正在为 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')
4

2 回答 2

0

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

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

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

如果您需要获取选项 ID,请查看

于 2013-07-20T19:20:18.277 回答
0

你可以试试这个来过滤你的产品集合

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(*);
$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 'Yes'));
于 2013-07-20T10:51:34.140 回答