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

How to get product name in magento query?

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

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

这是我的查询。

$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

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

尝试使用 foreach 循环

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

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

如果您知道产品的产品 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 
?> 

这是获取带有特定类别名称和产品 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)