我有一家商店,里面有可配置且简单的产品(多种颜色)。目前,我们选择一个简单的产品图像并将其分配给可配置产品,这就是列表页面上显示的内容。问题是,如果该特定颜色缺货,我们就会被一张代表不可用产品的图像卡住(并且必须手动更新该图像)。
有没有办法在列表页面上使用简单的产品图像,同时仍然允许控制使用哪个图像?我知道如何在列表页面上使用简单图像,但我不知道如何指定哪个简单图像(目前我只是抓取简单产品并从列表中的第一个中提取图像)。
如果我能找到一种方法对可配置产品中的简单产品进行排序(即,确保产品 A 的简单产品按黑色、绿色、蓝色排序,而产品 B 的简单产品按绿色、蓝色、黑色排序) ,我想我可以弄清楚其余的。
有任何想法吗?
最佳答案
弄清楚了。我为简单的产品添加了一个新属性,称为“sort_order”。然后,我覆盖了目录/产品助手并添加了以下方法:
public function getSortedSimpleProducts($product) {
$products = array();
$allProducts = $product->getTypeInstance(true)->getUsedProducts(null, $product);
foreach ($allProducts as $product) {
if ($product->isSaleable()) {
$products[] = $product;
}
}
$sorted_products = array();
$unsorted_products = array();
foreach ($products as $simple_product) {
$sort_order = $simple_product->getData('sort_order');
if ($sort_order) {
$sorted_products[$sort_order] = $simple_product;
}
else {
$unsorted_products[] = $simple_product;
}
}
$final_products = $sorted_products;
if (count($unsorted_products) > 0) {
$final_products = array_merge($sorted_products, $unsorted_products);
}
if (count($final_products) > 0) {
sort($final_products);
}
return $final_products;
}
然后,在 list.phtml 模板中,围绕这一行:
<?php $i=0; foreach ($_productCollection as $_product): ?>
我添加了以下代码:
$image_product = $_product;
$products = $this->helper('catalog/product')->getSortedSimpleProducts($_product);
if (count($products) > 0) {
$image_product = $products[0];
}
并更新了我的图片标签:
<img src="<?php echo $this->helper('catalog/image')->init($image_product, 'small_image')->resize(189,238); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
然后我覆盖 Mage_Catalog_Block_Product_View_Type_Configurable 以使 getAllowProducts 按新的排序顺序排序(这决定了 View 页面上颜色的排序):
public function getAllowProducts() {
if (!$this->hasAllowProducts()) {
$products = array();
$allProducts = Mage::helper('catalog/product')->getSortedSimpleProducts($this->getProduct());
foreach ($allProducts as $product) {
if ($product->isSaleable()) {
$products[] = $product;
}
}
$this->setAllowProducts($products);
}
return $this->getData('allow_products');
}
然后更新了 media.phtml 文件:
$childProducts = $this->helper('catalog/product')->getSortedSimpleProducts($_product);
这样产品图像也将使用相同的排序。
我希望这不会对性能产生巨大影响(客户表示这是一项主要要求)。如果客户没有在简单产品上设置排序顺序,它会很好地降级。并且,如果库存缺货,它将按排序顺序显示下一个图像。
欢迎任何批评!
关于magento 在列表页面上使用简单的产品图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6462654/