【问题标题】:Get all products data in magento获取magento中的所有产品数据
【发布时间】:2013-01-08 10:03:43
【问题描述】:

您好,我从我的代码中获得了 particular *product* 数组,用于特定 product id ,我想要 strong> 获取 all *products 数组*..... 我的代码是

**<?php
@ob_start();
@session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId);  //load the product     
echo 'product_id '.'=>'.$product->getId().'<br/>';                                        
?>**

【问题讨论】:

    标签: php arrays zend-framework magento get


    【解决方案1】:

    你想要什么? $product 不是数组,它是一个对象。如果您想拥有所有产品,您可以使用此代码:

    $collection = Mage::getResourceModel('catalog/product_collection');
    foreach($collection as $product) {
        echo $product->getId();
    }
    

    【讨论】:

    • 谁能告诉我如何获取所有产品数据
    【解决方案2】:

    这里我放了如何获取所有产品的名称,同样你可以得到你想要的属性。

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

    【讨论】:

      【解决方案3】:
      function products($category_id){
          $json = array('success' => true, 'products' => array());
          $category = Mage::getModel ('catalog/category')->load($category_id);
          $products = Mage::getResourceModel('catalog/product_collection')
                        // ->addAttributeToSelect('*')
                        ->AddAttributeToSelect('name')
                        ->addAttributeToSelect('price')
                        ->addFinalPrice()
                        ->addAttributeToSelect('small_image')
                        ->addAttributeToSelect('image')
                        ->addAttributeToSelect('thumbnail')
                        ->addAttributeToSelect('short_description')
                        ->addUrlRewrite()
                        ->AddCategoryFilter($category);
          Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
          $currencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
          foreach($products as $product){ 
              $json['products'][] = array(
                      'id'                    => $product->getId(),
                      'name'                  => $product->getName(),
                      'description'           => $product->getShortDescription(),
                      'pirce'                 => Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode,
                      'href'                  => $product->getProductUrl(),
                      'thumb'                 => (string)Mage::helper('catalog/image')->init($product, 'thumbnail')
                  );
          }
          return $json;
      }
      

      完整的插件 REST API 见 https://github.com/app-z/magento-android-web-api/blob/master/web-api.php

      【讨论】:

        【解决方案4】:

        也许是这样的?

        $collection = Mage::getResourceModel('catalog/product_collection');
        foreach ($collection as $product) {
        
            $data = $product->getData();
            foreach ($data as $d)
                var_dump($d);
        
        }
        

        【讨论】:

          【解决方案5】:
          $term = Mage::helper('catalogsearch')->getQueryText();
              $query = Mage::getModel('catalogsearch/query')->setQueryText($term)->prepare();
              $fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
                  Mage::getModel('catalogsearch/fulltext'),
                  $term,
                  $query
              );
          
              $collection = Mage::getResourceModel('catalog/product_collection');
              $collection->getSelect()->joinInner(
                  array('search_result' => $collection->getTable('catalogsearch/result')),
                  $collection->getConnection()->quoteInto(
                      'search_result.product_id=e.entity_id AND search_result.query_id=?',
                      $query->getId()
                  ),
                  array('relevance' => 'relevance')
              );
          
              $productIds = array();
              $productIds = $collection->getAllIds(); // as per Amit Bera s' comment
          Zend_Debug::dump($productIds);
          

          【讨论】:

            猜你喜欢
            • 2012-02-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多