0

I'm trying to get product collection and join table catalog/category_product.

$productCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect('*')
                        ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
                        ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left');

And I have the error. Item (Mage_Catalog_Model_Product) with the same id "338" already exist. What am I doing wrong? Thanks.

  • one product can be from multiple category – Gopal Patel Apr 14 '17 at 9:53
  • I understand that. And how can I filter my product collection by product id? – Nikolai Silin Apr 14 '17 at 10:32
  • what exatly you want to achieve? – Gopal Patel Apr 14 '17 at 10:33
  • I want to take all products in all stores and adding to them category ids. I try to use ->addCategoryIds(), but it doesn't work. May be Iim doing some mistake. – Nikolai Silin Apr 14 '17 at 11:31
3

Please use this collection.

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToSelect('*');
  • I tried this method. I had the error: Notice: Undefined index: l in /var/www/magento/public_html/app/code/core/Mage/ConfigurableSwatches/Helper/Productimg.php on line 145 – Nikolai Silin Apr 14 '17 at 11:03
  • And I think it's the same like my version. – Nikolai Silin Apr 14 '17 at 11:14
  • I'm sorry. You are right. My error is not related to this. I fixed problem, but I still use ResourceModel. – Nikolai Silin Apr 14 '17 at 11:21
0

My category id is

<?php $catid=10 ;?>
I got all product by writing the below code

    <?php
    $category = new Mage_Catalog_Model_Category();
    $category->load($catid); //My cat id is 10
    $prodCollection = $category->getProductCollection();
    foreach ($prodCollection as $product) {
    $prdIds[] = $product->getId(); ///Store all th eproduct id in $prdIds array
    }?>

Now you have all the product ids in `$prdIds` variable. 

Just click here to get all details of the product from a particular product id.

You can get individual product id by the following loop.

    <?php
    foreach($prdIds as $_prdIds){
    $product_id = $_prdIds;
    $obj = Mage::getModel(‘catalog/product’);
    $_product = $obj->load($product_id); // Enter your Product Id in $product_id
    // get Product’s name
    echo $_product->getName();
    //get product’s short description
    echo $_product->getShortDescription();
    //get Product’s Long Description
    echo $_product->getDescription();
    //get Product’s Regular Price
    echo $_product->getPrice();
    //get Product’s Special price
    echo $_product->getSpecialPrice();
    //get Product’s Url
    echo $_product->getProductUrl();
    //get Product’s image Url
    echo $_product->getImageUrl();
    }
    ?>

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.