所以,我试图在网格页面上显示颜色样本。
如果没有此代码,页面会在3秒左右呈现。
有了它,它会跳到12-15秒。
if($_product->isConfigurable())
{
$cIds = Mage::getModel('catalog/product_type_configurable')->getUsedProductIds($_product); // Get all associated product ids
// Pull a list of attributes we need for each simple product
$cProd = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id',array('in'=>$cIds))
->addAttributeToSelect(array('color','color_family','color_tile','small_image','entity_id'))
->setOrder("size")
->load();
$colors = array();
$swatches = "";
foreach($cProd as $_cProd)
{
if($_cProd->isSaleable()) // check if it's able to be sold
{
$c=$_cProd->getColor();
if(!isset($colors[$c])) // check if we've seen this color before
{
$swatches .= "<div class = 'swatch'><img src='/media/catalog/product/".$_cProd->getColorTile()."' title='".$_cProd->getAttributeText("color")."' full='".$_cProd->getSmallImage()."' onmouseover='switchColor(this,\"mainimage-".$pid."\")'></div>";
$colors[$c] = true;
}
}
}
}
我还尝试通过将->load()
切换为->group('color')->getSelect()
来对集合进行分组,但这会引发错误或返回空白集。
我尝试了多种方法,但我不能加快速度。我错过了什么?我可以尝试不同的方法吗?
答案 0 :(得分:0)
这很疯狂。您正在为列表中的每个产品加载一个集合,因此在昂贵地加载集合20或30次之后,最终可能会运行foreach循环20或30次。
方法1:
/**
* @param Mage_Catalog_Model_Product $product
* @return Varien_Data_Collection
*/
public function getFastUsedProducts(Mage_Catalog_Model_Product $product, Array $attributes = array())
{
$catalogResource = Mage::getResourceModel('catalog/product_collection');
if ($catalogResource->isEnabledFlat()) {
$resource = Mage::getResourceSingleton('xxx/catalog_product_type_configurable');
$productRows = $resource->getFastUsedProducts($product, $attributes);
$products = new Varien_Data_Collection();
if (!empty($productRows)) {
foreach ($productRows as $productData) {
$productData['id'] = $productData['entity_id'];
$product = Mage::getModel('catalog/product');
$product->setData($productData);
$products->addItem($product);
}
}
} else {
// FALLBACK TO SLOW RETRIEVAL
$products = $product->getTypeInstance()->getUsedProducts();
}
return $products;
}
和getFastUsedProducts的资源模型:
public function getFastUsedProducts(Mage_Catalog_Model_Product $product, Array $attributes = array())
{
if (!in_array('entity_id', $attributes)) $attributes[] = 'entity_id';
/** @var Varien_Db_Adapter_Pdo_Mysql $readConnection */
$readConnection = Mage::getSingleton('core/resource')->getConnection(Mage_Core_Model_Resource::DEFAULT_READ_RESOURCE);
/* @var $select Varien_Db_Select */
$select = $readConnection
->select()
->from(
array(
'r' => Mage::getResourceSingleton('catalog/product_relation')->getMainTable()
),
array('r.child_id')
)
->where('r.parent_id = ?', (int) $product->getId());
$childIds = $readConnection->fetchCol($select);
if (empty($childIds)) return NULL;
/** @var Mage_Catalog_Model_Resource_Product_Flat $flatTable */
$flatTable = Mage::getResourceSingleton('catalog/product_flat');
$availableAttributes = array_intersect($attributes, $flatTable->getAllTableColumns());
/* @var $select Varien_Db_Select */
$select = $readConnection
->select()
->from(
Mage::getResourceSingleton('catalog/product_flat')->getMainTable(),
$availableAttributes
)
->where('entity_id IN (?)', $childIds);
$productRows = $readConnection->fetchAll($select);
return $productRows;
}
/**
* Retrieve simples grouped by an attribute code
*
* @param Mage_Catalog_Model_Product $product
* @param $attributeCode
* @return array
*/
public function getGroupedSimples(Mage_Catalog_Model_Product $product, $attributeCode, Array $selectAttributes)
{
if (isset($this->_groupedSimplesMap[$product->getId()][$attributeCode])) {
return $this->_groupedSimplesMap[$product->getId()][$attributeCode];
}
$index = array();
$simples = Mage::getSingleton('xxx/catalog_product_type_configurable')->getFastUsedProducts($product, $selectAttributes);
foreach ($simples as $simple) {
$value = $simple->getData($attributeCode);
if ($value == '' || isset($index[$value])) continue;
$index[$value] = $simple;
}
$this->_groupedSimplesMap[$product->getId()][$attributeCode] = $index;
return $index;
}
和
<?php if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) : ?>
<?php $_colorSimples = $this->helper('xxx/product_type_configurable')->getGroupedSimples($_product, 'color'); ?>
<?php $_showSidebar = count($_colorSimples) > 1; ?>
<?php endif; ?>
<div
class="item-wrap <?php echo $_product->getTypeId(); ?><?php if ($_showSidebar) : ?> sb<?php endif; ?>">
<div class="item-inner-wrap">
<?php if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) : ?>
<?php if ($_showSidebar) : ?>
<div class="additional-info simples-wrap">
<ul class="simples">
<?php foreach ($_colorSimples as $_simple) : ?>
<li class="simple-item"
data-product-id="<?php echo $_simple->getId(); ?>"><img
src="<?php echo $this->helper('catalog/image')->init($_simple, 'thumbnail')->resize(46); ?>"
data-grid-src="<?php echo $this->helper('catalog/image')->init($_simple, 'small_image')->resize(226); ?>"
data-zoom-image="<?php echo $this->helper('catalog/image')->init($_simple, 'small_image')->resize(640); ?>"
width="46" height="46"
alt="<?php echo $this->stripTags($this->getImageLabel($_simple, 'small_image'), null, true) ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
....