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