Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

addCategoryFilter not working in external php script
 
maes100
Newbie
 
Total Posts:  0
Joined:  2013-02-27
 

Hello,

I have written a function in PHP to get all products from a category in Magento to use in our own cms outside magento.  The problem is that our code works but is very heavy for our server because first I get all products and afterwards I filter them on categoryname.

Reason is that the addCategoryFilter(’10’) or addCategoryFilter(10) is not working and returns nothing.  (my category is definitely 10)

Does anyone have occured the same problem and can help with a workaround?

Thanks a lot
Steven

<?php

function get_products_by_catname($catname){

require_once("app/Mage.php");
Mage::app();

//GET ALL PRODUCTS ////////////////////////////////////////////////////////////////////////////

$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array('name', 'small_image','special_price','special_from_date','special_to_date','price','url_key', 'is_in_stock', 'is_salable', 'short_description', 'to_order'))
->setOrder('name','asc')
->load();

//GET ALL PRODUCT CATEGORIES FOR EVERY PRODUCT TO FILTER ////////////////////////////////////////////////////////////////////////////

foreach ($collection as $product) {

foreach ($product->getCategoryIds() as $category_id) {

$productCategory = Mage::getModel('catalog/category')->load($category_id);

if($productCategory->getName() == $catname){

echo '.  ///////// productname: '.$product->getName().'<br />';

}
}
}
}

get_products_by_catname('Bracelets');
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
devromans
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2012-04-05
 

Hello, you can use something like this:

$categories Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();
foreach (
$categories as $category)
{
    
if (strtolower($category->getName()) == "bracelets"{
        $productColletion 
$category->getProductCollection();
        break;
    
}
}

foreach ($productColletion as $product{
  
echo $product->getId()."\r\n";
}

P.S. I have not tested this code smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top