我在Magento CE 1.8.1.0中安装了Magento产品部件交互图扩展。
问题是它只为每个产品呈现一个单独的零件图。我需要它来渲染每个产品的所有图表。
代码是:
public function getPartDiagram()
{
if ($this->getId()) {
return Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->load($this->getId());
}
if (Mage::registry('product')) {
$collection = Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->getCollection()
->addFieldToFilter('sku', Mage::registry('product')->getSku());
return $collection->getFirstItem();
}
return null;
}
如果我return $collection->getLastItem();
它可以工作,最后一项按预期在前端呈现。
但我似乎无法渲染所有项目,getItems
无效。
代码文件如下,不确定但可能有帮助。
<?php
/**
* Catalog product parts list block
*
* @category PWS
* @package ProductPartsDiagram
* @author Anda Bardeanu <info@pandawebstudio.com>
*/
class PWS_ProductPartsDiagram_Block_List extends Mage_Catalog_Block_Product_Abstract
{
protected $_productsCount = null;
const DEFAULT_PRODUCTS_COUNT = 5;
protected $_columnCount = 4;
protected $_items;
protected $_itemCollection;
protected $_itemLimits = array();
/**
* Initialize block's cache
*/
protected function _construct()
{
parent::_construct();
$this->addColumnCountLayoutDepend('empty', 6)
->addColumnCountLayoutDepend('one_column', 5)
->addColumnCountLayoutDepend('two_columns_left', 4)
->addColumnCountLayoutDepend('two_columns_right', 4)
->addColumnCountLayoutDepend('three_columns', 3);
$this->addData(array(
'cache_lifetime' => 86400,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
));
if (!$this->getData('template')) {
$this->setTemplate('pws_productpartsdiagram/list.phtml');
}
}
/**
* Get Key pieces for caching block content
*
* @return array
*/
public function getCacheKeyInfo()
{
return array(
'CATALOG_PRODUCT_PARTS_'.$this->getPartDiagram()->getId(),
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
'template' => $this->getTemplate(),
$this->getProductsCount()
);
}
/**
* Prepare collection with new products and applied page limits.
*
* return Mage_Catalog_Block_Product_New
*/
protected function _beforeToHtml()
{
$partDiagramId = -1;
$partDiagram = $this->getPartDiagram();
if ($partDiagram) {
$partDiagramId = $partDiagram->getId();
}
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$resource = Mage::getSingleton('core/resource');
$productsTable = $resource->getTableName('pws_productpartsdiagram_products');
// no pagination
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter();
//->setPageSize($this->getProductsCount())
//->setCurPage(1);
$collection->getSelect()
->joinInner(
array('_table_products' => $productsTable),
'_table_products.product_id = e.entity_id',
array()
)
->from("", array('partdidagram_product_id'))
->where('_table_products.partdiagram_id = '. (int) $partDiagramId);
$this->setProductCollection($collection);
$this->_itemCollection = $collection;
return parent::_beforeToHtml();
}
/**
* Set how much product should be displayed at once.
*
* @param $count
* @return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
public function getPartDiagram()
{
if ($this->getId()) {
return Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->load($this->getId());
}
if (Mage::registry('product')) {
$collection = Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->getCollection()
->addFieldToFilter('sku', Mage::registry('product')->getSku());
return $collection->getFirstItem();
}
return null;
}
/**
* Get how much products should be displayed at once.
*
* @return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
public function getItemCollection()
{
return $this->_itemCollection;
}
public function getItems()
{
if (is_null($this->_items)) {
$this->_items = $this->getItemCollection()->getItems();
}
return $this->_items;
}
public function getRowCount()
{
return ceil(count($this->getItemCollection()->getItems())/$this->getColumnCount());
}
public function setColumnCount($columns)
{
if (intval($columns) > 0) {
$this->_columnCount = intval($columns);
}
return $this;
}
public function getColumnCount()
{
return $this->_columnCount;
}
public function resetItemsIterator()
{
$this->getItems();
reset($this->_items);
}
public function getIterableItem()
{
$item = current($this->_items);
next($this->_items);
return $item;
}
/**
* Set how many items we need to show in block
* Notice: this parametr will be also applied
*
* @param string $type
* @param int $limit
* @return Mage_Catalog_Block_Product_List_Upsell
*/
public function setItemLimit($type, $limit)
{
if (intval($limit) > 0) {
$this->_itemLimits[$type] = intval($limit);
}
return $this;
}
public function getItemLimit($type = '')
{
if ($type == '') {
return $this->_itemLimits;
}
if (isset($this->_itemLimits[$type])) {
return $this->_itemLimits[$type];
}
else {
return 0;
}
}
}
答案 0 :(得分:0)
我想你只想访问图表集合。因此,将此代码添加到产品view.phtml
文件中可能最简单:
$collection = Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->getCollection()
->addFieldToFilter('sku', Mage::registry('product')->getSku());
foreach($collection as $productpartsdiagram){
//code to display each diagram
}
或者扩展具有公共函数getPartDiagram()的类并添加自己的函数:
public function getPartDiagramsAll()
{
if (Mage::registry('product')) {
$collection = Mage::getModel('pws_productpartsdiagram/productpartsdiagram')->getCollection()
->addFieldToFilter('sku', Mage::registry('product')->getSku());
return $collection;
}
return null;
}
调用此函数,然后遍历返回的集合。