0

For my Magento shop, I'm trying to retrieve all products that have an attribute set to yes called "uitgelicht_categorie" under the categories. The products should be shown depending on the categories. Meaning that if a product is attached to a category with ID 1 and in that category a sub category with ID 3, the product shouldn't be shown in a category with ID 2.

On my test environment with Magento version 1.9.2.4 this works fine, but the exact same code does not work on my live environment with Magento version 1.9.2.1. I've checked the settings as well, but these seem to be the exact same. All the selected products are turned on and showing on the rest of the shop as well.

Here is the code I'm using:

$yesOpID=null;
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'uitgelicht_categorie');
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $e){
        if($e['label']=='Yes'):
            $yesOpID=$e['value'];
        endif;
    }
}

// get the current category
$currentCategory = Mage::registry('current_category');

if($currentCategory->getProductCount() == 0 ):
    // Instantiate a product collection object
    $products = Mage::getModel('catalog/product')->getCollection();

    // Select which fields to load into the product
    // * will load all fields but it is possible to pass an array of
    // select fields to load
    $products->addAttributeToSelect('*');

    $subCategories = explode(',', $currentCategory->getAllChildren());

    $products->addAttributeToFilter('uitgelicht_categorie', $yesOpID);
    $products->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
    $products->addAttributeToFilter('category_id', array('in' => array('finset' => $subCategories)));
    $products->getSelect()->group('e.entity_id');

    $products->getSelect()->order('RAND()');
    $products->getSelect()->limit(8);

    if(count($products) != 0):
?>
<h2 class="subtitle text-center"><em><?php echo $this->__('Meest populair in deze categorie'); ?></em></h2>
<ul class="products-grid">
    <?php
        foreach ($products->load() as $product):
0

It looks like $yesOpID is null on your live site because no option with value "Yes" is found. I guess, the relevant difference between your environments is not the Magento version but the localization.

Either way, this way to filter a "yes/no" attribute is not only unstable but also inefficient.

I suggest you to update the attribute to use the eav/entity_attribute_source_boolean source model.

Then you can filter simply by:

$products->addAttributeToFilter('uitgelicht_categorie', 1);
| improve this answer | |
  • How does this eav/entity_attribute_source_boolean work? I don't find a lot of information on it. – user3478148 Sep 2 '16 at 13:00
  • I noticed now that it's the $products->addAttributeToFilter('category_id', array('in' => array('finset' => $subCategories))); part that's not working. It doesn't include the category id's in the query. I also tried replacing the variable $subCategories with the actual ID's, but this didn't make a difference. – user3478148 Sep 2 '16 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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