3

We have a LOT of sizes in our database. This makes a size filter near impossible unless we narrow down the filter to the possible values found in the category the user is browsing.

Is it possible to return an array or list made from only the sizes that are available in the products of a certain category?

1

You can get sizes that are available in the products of a certain category by this code:

$categoryId = 1;
$category = Mage::getModel('catalog/category')->load($categoryId);

$collection = $category->getProductCollection();
$collection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$collection->addAttributeToFilter('size', array('neq' => null));
$collection->addFieldToFilter('visibility', array(
    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, 
    Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
));
$collection->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.is_in_stock=1',
    'left'
);
$collection->addAttributeToFilter('qty', array('gteq' => 1));
$collection->getSelect()->group('size');

$sizeCollection = array();
foreach ($collection as $product) {
    $sizeCollection[] = $product['size'];
}

echo '<pre>';
print_r($sizeCollection);
die;
  • Won't this load up all the products though? How will this be performance wise? Edit: Nevermind, just noticed you grouped by size. Thank you! – Pat Jun 6 '17 at 13:26
  • Going to try it asap and mark the answer correct. :) – Pat Jun 6 '17 at 13:58

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.