首页>Program>source

有人知道我如何从 Magento 的视图文件中获得属于特定类别的产品列表. a>?

最新回答
  • 11天前
    1 #

    您可以使用magento对象进行过滤。

    示例:

    $categoryId = 123; // a category id that you can get from admin
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addCategoryFilter($category)
        ->load();
    print_r($products);
    

  • 11天前
    2 #

    这完全取决于您所在的视图。;-)

    首先,我希望您能使用模板集(在我的示例中为默认值)。

    使用它作为 example

    <?php
    $_cat         = $this->getCurrentCategory();
    $_parent      = $_cat->getParentCategory();
    $_categories  = $_parent->getChildren();
    /* @var $category Mage_Catalog_Model_Category */
    $collection = Mage::getModel('catalog/category')->getCollection();
    /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
    $collection->addAttributeToSelect('url_key')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('is_anchor')
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($_categories)
        ->setOrder('position', 'ASC')
        ->joinUrlRewrite()
        ->load();
    $productCollection = Mage::getResourceModel('catalog/product_collection');
    $layer             = Mage::getSingleton('catalog/layer');
    $layer->prepareProductCollection($productCollection);
    $productCollection->addCountToCategories($collection);
    // $productCollection should be ready here ;-)
    ?>
    

    我正在使用上面的代码在模板中显示姐妹类别-虽然不理想,但是可以使用。

    这有点像黑客,因为我还没有时间学习所有的布局XML疯狂.否则,如果您使用XML,则需要牢记-这完全取决于您所在的位置. Where 表示模板文件,实际上也表示布局(就app / design / frontend / default / default / default / layout / *而言)。

    我知道不多,但是希望对您有所帮助。

  • 11天前
    3 #

    以下是获取任何特定类别产品的代码.您也可以在视图文件中使用它。

    // if you want to display products from current category
    $category = Mage::registry('current_category'); 
    // if you want to display products from any specific category
    $categoryId = 10;
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $productCollection = Mage::getResourceModel('catalog/product_collection')
                                     ->addCategoryFilter($category);
    // printing products name
    foreach ($productCollection as $product) {
        echo $product->getName(); 
        echo "<br />";
    }
    

  • 11天前
    4 #

    <?php
    $c_id = 2;
    $category = new Mage_Catalog_Model_Category();
    $category->load($c_id);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');
    foreach ($collection as $_product) { ?>
    <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
    <?php } ?>
    

  • 11天前
    5 #

    我几乎需要同样的东西.这是我的操作方式:

    $prod_whole = array();
    if(!empty($_menu)) //$_menu = array of Categories with some basic info
    foreach($_menu as $v)
    {
        if($v['name']=='HOME')
        continue;
        $cat_id = $v['id'];
        #/ Setup Products
        $category = Mage::getModel('catalog/category')->load($cat_id);
        $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*') // select all attributes
        ->addCategoryFilter($category)
        ->setPageSize(8) // limit number of results returned
        ->setCurPage(0)
        ->load()
        ;
    
        $prod_collection = array();
        foreach ($collection as $product)
        {
          $prod_collection_1 = array();
          #/ Basic Info
          $prod_collection_1['id'] = $product->getId();
          $prod_collection_1['name'] = $product->getName();
          $prod_collection_1['price'] = (float) $product->getPrice();
          //$prod_collection_1['desc'] = $product->getDescription();
          //$prod_collection_1['short'] = $product->getShortDescription();
          $prod_collection_1['type'] = $product->getTypeId();
          $prod_collection_1['status'] = $product->getStatus();
          $prod_collection_1['special_price'] =  $product->getSpecialPrice();
          $prod_collection_1['direct_url'] =  $product->getProductUrl();
    
          #/ getCategoryIds(); returns an array of category IDs associated with the product
          foreach ($product->getCategoryIds() as $category_id)
          {
              $category = Mage::getModel('catalog/category')->load($category_id);
              $prod_collection_1['parent_category'] = $category->getParentCategory()->getName();
              $prod_collection_1['category'] = $category->getName();
              //$prod_collection_1['category_idx'] = preg_replace('/[\s\'\"]/i', '_', strtolower(trim($prod_collection_1['category'])));
              $prod_collection_1['category_id'] = $category->getId();
          }
          #/gets the image url of the product
          $prod_collection_1['img'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
    
          $prod_collection[] = $prod_collection_1;
        }//end foreach.....
        $prod_whole[$cat_id] = $prod_collection;
    }//end foreach categories.......
    //var_dump('<pre>', $prod_whole);
    

    希望这会有所帮助。

  • mips:add和addu之间的区别
  • class:在NET中将成员对象公开为属性或方法