如何在magento查询中获取产品名称?

时间:2018-02-27 04:19:38

标签: php mysql magento magento-1.9

您好我知道这是一个简单的问题,但我在这里没有得到任何解决方案。所以我要问。

我正在使用magento 1.9,我想要一个名称和特定类别的产品网址的产品列表。我尝试了很多,但我没有得到产品名称和产品网址。

这是我的问题。

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

输出

            [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

那么如何在此查询中获取产品名称和产品网址?

2 个答案:

答案 0 :(得分:3)

尝试使用foreach循环

$collection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*');

foreach ($collection as $product) {
 echo $product->getName() . "<br />";
}

OR

如果您知道产品的产品ID

<?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 
?> 

答案 1 :(得分:0)

以下是获取具有特定类别的名称和产品Url的产品列表的方法。

$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;
        }

在其后面执行的相应MySQL查询是:

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)