10

这是这种情况:

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

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

我的一些产品在 1 个可配置产品中有 30 个简单的产品,上传相同的图像 30 次是矫枉过正/烦人的。

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

提前致谢!

4

4 回答 4

11

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

$_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]);
}

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

编辑

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

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

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

HTH,
法学博士

于 2011-02-21T00:06:41.243 回答
4

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

于 2011-03-23T19:55:02.687 回答
2

一个快速的解决方法可能是导出您的产品列表(Admin > System > Import/Export > Profiles),将图像文件名放在所有简单产品的相应列中,将文件复制到media/import/目录然后导入修改后的产品列表。将为您建立各种关联,并将图像文件复制到需要的位置。

于 2011-02-20T23:36:31.623 回答
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);
    }
}
于 2012-05-11T15:47:17.547 回答