1

I'm trying to set up a form in an external that will give me all Sku's of configurable or simple products in a category entered by the user. Instead, I'm ending up with a list of all products in the store, so my filter doesn't seem to be working:

<?php

function getProductSkus(){
    if(isset($_POST['submit'])){
        $categoryId = $_POST['categoryId'];
        $productType = $_POST['productType'];
        require_once('../app/Mage.php');
        Mage::app();
        $category = Mage::getModel('catalog/category')->load($categoryId);
        if($category){
            $productList = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('type_id', $productType)
                ->addCategoryFilter($category)
            foreach($productList as $product){
                echo $product->getName() . "<br>";
            }
        }
    }
}
?>

<html>
<body>

<form action="getproducts.php" method="post">
<p>Category ID: <input name="categoryId" type="text" /></p>
<p>Product Type: <select name="productType">
    <option value="configurable">Configurable</option>
    <option value="simple">Simple</options>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
<?php getProductSkus(); ?>
</body>
</html>

Can anyone tell me what I'm doing wrong here? I'm essentially following the method here, but using load() after the category filter doesn't seem to have any effect:

https://stackoverflow.com/questions/29124232/magento-get-all-products-in-the-current-category

I've also used the prepareProductCollection method here with the same results:

https://stackoverflow.com/questions/14318488/magento-get-all-products-in-a-certain-category-ordered-by-the-position-field

1

I found the problem. It turns out that the current store has to be set where I currently have the app initialized at Mage::app();.

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
0

I think Below code solve your concern

$collection = Mage::getModel('catalog/category')
                ->load($categoryId)
                ->getProductCollection();
 foreach($collection as $product){
    echo $product->getSku();
 }  
  • Using mysql how can i get Product sku, name by selected category id? @Murtuza Zabuawala – Gem Dec 9 at 7:39

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.