首页 文章

Magento - 如何将可配置产品图像链接到简单产品图像?

提问于
浏览
10

情况就是这样:

我有一个可配置的产品,有几个简单的产品 . 这些简单的产品需要与可配置产品具有相同的产品图像 . 目前,我必须一遍又一遍地将相同的图像上传到每个简单的产品 .

有没有办法将可配置产品的产品图像链接到简单产品?

我的一些产品在一个可配置产品中有30个简单的产品,上传相同的图像30次是太过分/烦人 .

我希望有人可以帮我解决这个问题!

提前致谢!

4 回答

  • 11

    $_product = $this->getProduct(); 之后将其插入 DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\view\media.phtml

    $_parentIdArray = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
    if(sizeof($_parentIdArray)==1 && Mage::getModel('catalog/product')->load($_parentIdArray[0])->getTypeId() == 'configurable'){
      $_product = Mage::getModel('catalog/product')->load($_parentIdArray[0]);
    }
    

    如果简单产品具有可配置类型的单个父级,则将使用属于父可配置产品的图像 .

    EDIT

    要在列表视图中使用它,请打开 DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\list.phtml 并在 2 位置插入相同的代码块:

    <?php foreach ($_productCollection as $_product): ?> 之后

    • 第45行( <?php ?> 包装内)
      <?php $i=0; foreach ($_productCollection as $_product): ?> 之后
    • 第106行(约,可能与您的主题不同)

    这两个位置都需要处理页面的网格视图和列表视图版本 .

    HTH,
    JD

  • 4

    我认为正确的方法是覆盖图像助手(app / core / Mage / Catalog / Helper / Image.php),这样在init函数中,你可以检查你是否有一个简单的产品,如果你做,用可配置的产品替换它 . 这应该会影响所有模板的更改 .

  • 2

    快速解决方法可能是导出产品列表(管理员>系统>导入/导出>配置文件),将图像文件名放在所有简单产品的相应列中,将文件复制到 media/import/ 目录然后导入修改后的产品列表 . 将为您制作各种关联,并将图像文件复制到他们需要的任何位置 .

  • 2

    我认为最好的方法是覆盖目录图像助手(如@stew所说) . 为避免性能问题,我们可以使用原始SQL查询来获取父级的图像值:

    class Wfs_Catalog_Helper_Image extends Mage_Catalog_Helper_Image
    {
        public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
        {
            parent::init($product, $attributeName, $imageFile);
            if (!$product->getId() || $imageFile || $product->isConfigurable()) {
                return $this;
            }
    
            $productImage = $product->getData($attributeName);
            if (!$productImage || $productImage == 'no_selection') {
                // Get parent product's attribute
                $value = $this->getParentProductAttribute($product->getId(), $attributeName);
                if ($value) {
                    $this->_getModel()->setBaseFile($value);
                }
            }
            return $this;
        }
    
        public function getParentProductAttribute($productId, $attributeName)
        {
            $coreResource = Mage::getSingleton('core/resource');
            $conn = $coreResource->getConnection('core_read');
    
            $attrId = Mage::getSingleton('eav/config')
                ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeName)
                ->getId();
    
            $select = $conn->select()
                ->from(array('rel'  => $coreResource->getTableName('catalog/product_relation')), array())
                ->join(
                    array('var' => $coreResource->getTableName('catalog_product_entity_varchar')),
                    'var.entity_id = rel.parent_id',
                    array('value')
                )
                ->where('rel.child_id = ?', $productId)
                ->where('var.attribute_id = ?', $attrId);
    
            return $conn->fetchOne($select);
        }
    }
    

相关问题