skip to Main Content

Hello I know this is simple question but I don’t get any solution in here. So I am going to ask.

I am using magento 1.9, and I want a product list with name and product Url of particular category. I tried a lot but I am not getting product name and product URL.

Here is my query.

$catid = $this->getCategory()->getId();
$_productCollection = Mage::getModel('catalog/category')->load($catid)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect('name')
            ->addFieldToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->joinField('is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'left');

Output

            [entity_id] => 1
            [entity_type_id] => 4
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => foo
            [has_options] => 1
            [required_options] => 1
            [created_at] => 2018-02-03 06:46:56
            [updated_at] => 2018-02-05 00:22:04
            [cat_index_position] => 1
            [status] => 1
            [visibility] => 4
            [is_in_stock] => 1

So how to get product name and product url in this query ?

2

Answers


  1. Try using foreach loop

    $collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('*');
    
    foreach ($collection as $product) {
     echo $product->getName() . "<br />";
    }
    

    OR

    If you know product ID of the product

    <?php 
     $model = Mage::getModel('catalog/product') //getting product model 
     $_product = $model->load($productid); //getting product object for particular product id 
     echo $_product->getName(); //product name 
     echo $_product->getProductUrl(); //product url 
    ?> 
    
    Login or Signup to reply.
  2. Here is the way to get product list with name and product Url of particular category.

    $category_id = 'xxxx';
    
            $category = Mage::getModel('catalog/category')->load($category_id);
    
            $collection = Mage::getResourceModel('catalog/product_collection')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->addCategoryFilter($category)
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('url_key');
    
            foreach ($collection as $product) {
                echo 'Name : ' . $product->getName() . ' --- ' . ' Product URL Key : ' . $product->getUrlKey() . PHP_EOL;
            }
    

    And the corresponding MySQL query that gets executed behind it is :

    SELECT `e`.*, `cat_pro`.`position` AS `cat_index_position`, `at_name`.`value` AS `name`, `at_url_key`.`value` AS `url_key` FROM `catalog_product_entity` AS `e`
     INNER JOIN `catalog_category_product` AS `cat_pro` ON cat_pro.product_id=e.entity_id AND cat_pro.category_id='xxxx'
     INNER JOIN `catalog_product_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '96') AND (`at_name`.`store_id` = 0)
     INNER JOIN `catalog_product_entity_varchar` AS `at_url_key` ON (`at_url_key`.`entity_id` = `e`.`entity_id`) AND (`at_url_key`.`attribute_id` = '481') AND (`at_url_key`.`store_id` = 0)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search