Magento Forum

   
Loading product collection with filters on Custom Attributes
 
mnaeem76
Jr. Member
 
Total Posts:  12
Joined:  2010-07-16
 

I have a very peculiar situation for which I spent almost whole day trying to figure out. While my problem got solved, I am not sure why was this a problem in the first place. Here’s what I was dong:

In a multi-site Magento installation,I created a custom attribute with Website scope.. The attribute name is display_home. The purpose of this attribute is to mark a product to be displayed on home page (more like a featured product). Used in product listing is also set to Yes so I can use it in addAttributeToFilter in my code. I also set the default value for this attribute to ‘N’ and kept the input type as text filed for simplicity.

Next, I set the value of this attribute to ‘Y’ for a couple of products to test out in my code. I also re-indexed the indexes. Next I wrote a piece of code to pull those products as below:

$pcollection Mage::getModel('catalog/product')->getCollection();
$pcollection->addAttributeToSelect('name');
$pcollection->addAttributeToSelect('display_homepage');
$pcollection->addAttributeToFilter('display_homepage', array('eq'=>'Y'));

$products $pcollection->load();
echo 
"Products count: " count($products);
I kept on getting 0 as output. Tried a lot with different variations but to no avail. I then tried to spit out the SQL statement prepared by the code using
echo $pcollection->getSelect()
This revealed the following SQL statement:
SELECT 
  
`e`.*,
  IF(
    
at_display_homepage.value_id 0,
    
at_display_homepage.value,
    
at_display_homepage_default.value
  
) AS `display_homepage
FROM
  
`catalog_product_entity` AS `e
  
INNER JOIN `catalog_product_entity_varchar` AS `at_display_homepage_default
    
ON (
      `
at_display_homepage_default`.`entity_id` = `e`.`entity_id`
    ) 
    AND (
      `
at_display_homepage_default`.`attribute_id` = '183'
    

    AND `
at_display_homepage_default`.`store_id` = 
  LEFT JOIN 
`catalog_product_entity_varchar` AS `at_display_homepage
    
ON (
      `
at_display_homepage`.`entity_id` = `e`.`entity_id`
    ) 
    AND (
      `
at_display_homepage`.`attribute_id` = '183'
    

    AND (
      `
at_display_homepage`.`store_id` = 75
    

WHERE (
    (
      IF(
        
at_display_homepage.value_id 0,
        
at_display_homepage.value,
        
at_display_homepage_default.value
      
) = 'Y'
    
)
  )

My first hint came from the INNER JOIN where default value is being picked up (against store_id 0). I changed the INNER JOIN to LEFT JOIN and started getting the results.

However, I wondered why didn’t Magento create a default value record in catalog_product_entity_varchar so that I don’t get empty result with the generated query’s inner join?

To make my code work, I had to explicitly set the new attribute’s value to ‘N’ for each product by selecting ‘Default Values’ from the Choose Store view combo box. 

The version of Magento in use is 1.6.2 CE. The question I have now is that why didn’t Magento automatically create a record for default value so each website/store view would use that as a fall back value and that the filter criteria would not fail? Is this by design and one has to explicitly set default values for Default configuration/values?

The other option would be to create attribute with Global scope but that value would be global then and one won’t be able to bifurcate value for each website/store in a multi site configuration.

I would like to know more on how to tackle such situation as I am still new to Magento and the fact that I wasted a lot of time today urges me to come to some solid conclusions.

 
Magento Community Magento Community
Magento Community
Magento Community
 
IndiesWebs
Sr. Member
 
Avatar
Total Posts:  189
Joined:  2013-09-18
 

Hi,
Check your attribute code again, because I can see that you had write ‘display_home’ and in code you have used the attribute name ‘display_homepage’. So they do not match. One more thing to check is when you get collection without this condition, the value of this attribute returns for all the product. Check your system.log file in which the printing is done.

$pcollection Mage::getModel ('catalog/product') ->getCollection ();
    
Mage::log ($pcollection ->getData());

Hope this will help you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nilavacins
Jr. Member
 
Total Posts:  7
Joined:  2011-11-10
 

Hi,
Check your attribute again , the attribute name is ‘display_homepage’. Check the value of the attribute.
If you know, what is the exact value of your atribute and how to get the value of your attribute then please go to the customer section of Magento . Lets just say You found an attribute name ‘Gender’ the option of the attribute is ‘Male’ and ‘Female’ . But The value is different. ‘Male’ value is ‘1’ and ‘Female’ value is ‘2’. For example i attached a screenshot.

Like this you find the real value of your custom attribite’s option.Then put it in below code.

$productCollection = Mage::getResourceModel(’catalog/product_collection’);
$productCollection->addFieldToFilter(’type_id’, $productType)
->addAttributeToFilter(’display_homepage’, array(’eq’ => attribute value));

Hope this will help you.

Image Attachments
Picture.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top