Magento:如何根据产品属性为产品显示不同的占位符图像


Magento: how to display a different placeholder image for a product based on product attribute

问题:如何自定义Magento以基于特定产品属性为产品使用不同的占位符图像?

更多信息:

  • 我知道如果产品没有任何特定的图像,Magento会使用占位符图像
  • 但是,我希望占位符图像根据自定义产品属性(例如零件类型(进行更改
  • Magento社区版本:1.7.0.2

例如:

  • 如果产品类型是锤子,并且不存在特定的产品图像:
    • 那么我希望占位符图像是一个普通的锤子图像
  • 如果产品类型是螺丝刀,并且不存在特定的产品图像:
    • 那么我希望占位符图像是一个通用的螺丝刀图像
  • 等,用于其他零件类型

票据

  • 我不想默认为类别图像
  • 我不想把图片上传到每个产品上
  • 我只是想扩展占位符功能,以便根据产品属性提供不同的图像

提前感谢

您可以使用以下代码获取产品的parent category_id

$_product = Mage::getModel('catalog/product')->load($_item->getId());
$ids = $_product->getCategoryIds();
$categoryId = (isset($ids[0]) ? $ids[0] : null);

您可以使用自定义if/else条件来显示占位符图像。。。

应该有一个问题如果一个产品有多个父类别,那么你想要什么

更新

$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
    echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}

现在,您也可以对属性使用相同的类别逻辑。。。

根据@Rob的信息,我创建了一个工作原型。以下是感兴趣的人的补丁:

第一个文件

diff --git a/magento/app/code/local/Mage/Catalog/Helper/Image.php b/magento/app/code/local/Mage/Catalog/Helper/Image.php
--- a/magento/app/code/local/Mage/Catalog/Helper/Image.php
+++ b/magento/app/code/local/Mage/Catalog/Helper/Image.php
@@ -349,7 +349,14 @@ class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract
             if ($this->getImageFile()) {
                 $model->setBaseFile($this->getImageFile());
             } else {
-                $model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir()));
+                // if part type is in the passed product then use it otherwise get it
+                if ($this->getProduct()->getPartType()) {
+                    $productPartType = $this->getProduct()->getPartType();
+                } else {
+                    $productPartType = Mage::getModel('catalog/product')->load($this->getProduct()->getId())->getPartType();
+                }
+
+                $model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir()), $productPartType);
             }
             if ($model->isCached()) {

第二个文件

diff --git a/magento/app/code/local/Mage/Catalog/Model/Product/Image.php b/magento/app/code/local/Mage/Catalog/Model/Product/Image.php
--- a/magento/app/code/local/Mage/Catalog/Model/Product/Image.php
+++ b/magento/app/code/local/Mage/Catalog/Model/Product/Image.php
@@ -260,7 +260,7 @@ class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
      * @param string $file
      * @return Mage_Catalog_Model_Product_Image
      */
-    public function setBaseFile($file)
+    public function setBaseFile($file, $partType=Null)
     {
         $this->_isBaseFilePlaceholder = false;
@@ -278,10 +278,23 @@ class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
             }
         }
         if (!$file) {
+            // check if part type placeholder file exists
+            if ($partType) {
+                $skinPartTypeBaseDir     = Mage::getDesign()->getSkinBaseDir();
+                $skinPartTypePlaceholder = "/images/catalog/product/placeholder/{$partType}.jpg";
+                $PartTypefile = $skinPartTypePlaceholder;
+            }
+
             // check if placeholder defined in config
             $isConfigPlaceholder = Mage::getStoreConfig("catalog/placeholder/{$this->getDestinationSubdir()}_placeholder");
             $configPlaceholder   = '/placeholder/' . $isConfigPlaceholder;
-            if ($isConfigPlaceholder && $this->_fileExists($baseDir . $configPlaceholder)) {
+
+            // Use part type placeholder image if it exists
+            if ($partType && file_exists($skinPartTypeBaseDir . $PartTypefile)) {
+                $baseDir = $skinPartTypeBaseDir;
+                $file = $PartTypefile;
+            }
+            elseif ($isConfigPlaceholder && $this->_fileExists($baseDir . $configPlaceholder)) {
                 $file = $configPlaceholder;
             }
             else {