简体   繁体   English

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

[英]How to get product name in magento query?

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. 我正在使用magento 1.9,并且想要一个包含名称和特定类别的产品网址的产品列表。 I tried a lot but I am not getting product name and product URL. 我尝试了很多,但是没有得到产品名称和产品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 ? 那么如何在此查询中获取产品名称和产品URL?

Try using foreach loop 尝试使用foreach循环

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

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

OR 要么

If you know product ID of the product 如果您知道产品的产品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 
?> 

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 : 在其后面执行的相应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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM