【问题标题】:Magento - addFieldToFilter doesn't choose the right productMagento - addFieldToFilter 没有选择正确的产品
【发布时间】:2013-07-20 10:32:27
【问题描述】:

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

【问题讨论】:

    标签: php mysql magento magento-1.7


    【解决方案1】:

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

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

    【讨论】:

    • 使用 addAttributeToFilter 而不是 addFieldToFilter 的建议是正确的,但是对于 doorconfig_enable 我必须使用布尔值来正确过滤,就像 Roman Shopin 建议的那样:$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 1)); 虽然其他属性仍然不起作用如果我按 ID 遍历它们:
    • foreach ($attributes as $key => $value) { $collection->addAttributeToSelect($key); $collection->addAttributeToFilter($key, array( 'eq' => Mage::getResourceModel('catalog/product') ->getAttribute($key) ->getSource() ->getOptionId($value) ) ); }
    【解决方案2】:

    什么类型的“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,请查看this

    【讨论】:

    • doorconfig_enable 是一个是/否属性,所以它是一个布尔值。我尝试使用 1 作为值并且它有效。但是对于循环,我仍然没有运气。我试图获取所有选择的属性选项的 ID,如下所示:
    • foreach ($attributes as $key => $value) { $collection->addAttributeToSelect($key); $collection->addAttributeToFilter($key, array( 'eq' => Mage::getResourceModel('catalog/product') ->getAttribute($key) ->getSource() ->getOptionId($value) ) ); }
    • 但并非所有属性 ID 似乎都返回,因为查询如下所示:
    • 调试 (7): 选择 1 AS status, e.entity_id, e.type_id, e.attribute_set_id, e.@978 @,e.doorconfig_color,e.doorconfig_color_value,e.doorconfig_type,e.doorconfig_type_value,doorconfig_size_value.@987654343.@,doorconfig_size,@987654342 e.doorconfig_remote,e.doorconfig_remote_value,e.doorconfig_digitalkeypad,e.doorconfig_digitalkeypad_value,e.@9876543554@,doorconfig_extraremotecontrol,@98764534跨度>
    • e.configdoor_addemergencylock, e.configdoor_addemergencylock_value, e.doorconfig_insideopeningbutton, e.doorconfig_insideopeningbutton_value, e,e.@98 @.doorconfig_window_value FROM catalog_product_flat_1 AS e WHERE (e.doorconfig_enable = 1) AND (e.doorconfig_color = '') AND (e.doorconfig_type = '') AND (e.doorconfig_size = '6') AND (e.doorconfig_remote = '') AND (e.doorconfig_digitalkeypad = '') AND (e.doorconfig_extraremotecontrol = '') AND (e.configdoor_addemergencylock = '') AND (e.doorconfig_insideopeningbutton = '') AND (e.doorconfig_window = '')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多