【问题标题】:Magento - How to link the Configurable Product image to the Simple Product image?Magento - 如何将可配置产品图像链接到简单产品图像?
【发布时间】:2011-06-30 22:56:35
【问题描述】:

情况是这样的:

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

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

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

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

提前致谢!

【问题讨论】:

  • 每个简单的产品是否只属于一个可配置的产品?
  • 是的,每个简单的产品都属于一个可配置的产品

标签: php image magento product configurable


【解决方案1】:

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

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

编辑

要在列表视图中使用它,请打开 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,
京东

【讨论】:

  • 谢谢乔纳森,它在产品页面上对我有用。是否可以在产品列表中进行这项工作?目录/产品/list.phtml?提前致谢!
  • @Jonathan Day - 忘了在我的评论中提及你的名字。我希望你能帮我解决这个问题。提前致谢!
  • @Jonathan Day 非常感谢!该解决方案完美运行!
  • 没问题,如果它是正确的解决方案,请随时单击我的答案旁边的勾号。这就是 StackOverflow 的工作原理:)
  • @Jonathan Day - 如果我只想在 list.phtml 中的简单产品中显示可配置产品的图像怎么办?此刻,简单的产品正在被可配置的产品所取代,我希望简单的产品留在那里。这些简单的产品需要与可配置产品相​​同的图像。那可能吗?提前致谢!
【解决方案2】:

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

【讨论】:

  • 当然,您的方法是最好的,因为在这种情况下,我们只需要在一个地方更改逻辑。但这可能会导致性能问题,因此需要为父产品实现一些缓存
  • 是的,好吧,不管你做什么,magento 似乎都是一个很大的性能问题 :)
【解决方案3】:

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

【讨论】:

  • 这会复制很多图片
  • 感谢您的回答,但我正在将 Jonathan 的解决方案用于产品视图页面。我希望他可以调整产品列表页面的脚本。
【解决方案4】:

我认为最好的方法是覆盖目录图像助手(如@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);
    }
}

【讨论】:

  • @gonzela2006 您可以创建自己的模块,并在其中覆盖图像助手。
  • 非常感谢。我可以请你检查这个问题stackoverflow.com/questions/19931820/…
  • 这不适用于 Magento 1.8 我想这就是 @gonzela2006 提出新问题的原因。
猜你喜欢
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
相关资源
最近更新 更多