0

I am currently programming a product configurator module for Magento. Everything is working fine so far besides the core functionality: choosing the right product from the database for the selected attributes. My attributes belong to an attribute group from which the form fields for my configurator are generated, the values I use are the admin fields. The shop language is swedish.

The shop contains one configurable product with simple products associated to it. The simple products don't show individually in the shop. Right now not all combinations of my attributes are existing but it should at least return the right product for one combination I know is existing. First it always returned null, right now it returns all products. How can I achieve to have just product be chosen?

First the attributes didn't even show in the query until I activated "Used in Product Listing".

Thanks in advance for any help :)

This is my getProduct method:

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;

}

The $attributes parameter contains the POST data which is correctly submitted:

[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" is a typo by my dear collegues, but still read correctly from the database (just if anyone wonders). As I am not good in SQL and not very experienced in Magento, I don't know whether my query is right or wrong:

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

1 Answer 1

1

Products are an EAV entity so try using

$collection->addAttributeToFilter($key,array('eq' => $value));

instead. Additionally check that your products are fully indexed under System > Index Management.

1
  • Your suggestion was right so far. Aditionally I had to use the current Store labels and get the IDs for the selection option before using addAttributeToFilter. But this caused another problem, I normally use the admin values for URL generation and now I have to use the swedish labels because $collection->addAttributeToFilter($key, array( 'eq' => Mage::getResourceModel('catalog/product') ->getAttribute($key) ->getSource() ->getOptionId($value) ) ); always uses the current store values and doesn't work for the admin values. Do you know a way to get the attribute option ID for the admin value? Jul 21, 2013 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.