【问题标题】:Magento - configurable product option imagesMagento - 可配置的产品选项图像
【发布时间】:2012-07-12 16:57:18
【问题描述】:

我目前正在为我的第一个 magento 构建中显示的产品选项获取图像。我已经为捆绑产品找到了 THIS,如下所示:

当构建选择的选项时,我正在获取相关图像(例如样本)的 URL。现在我正在尝试对可配置产品做同样的事情,但它似乎并不那么简单。

可配置产品由代表可用选项的每次迭代的简单产品构建而成。伟大的。我显然可以为每个简单的产品上传图片,这将是解决此问题的良好开端。

例如: 椅子有 3 种内饰和 2 种扶手选择(6 种简单产品)。 对于椅子 2/b,我上传了内饰样本 2 和扶手样本 b,并相应地标记它们。构建选项后,我会通过标签获取与每个简单产品相关联的图片网址(可能会获取该标签的所有图片并删除重复项或其他内容?)...

在 Magento 中,我看到了:

theme/catalog/product/view/type/option/configurable.phtml

<?php foreach($_attributes as $_attribute): ?>
    ..// 
    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
        <option><?php echo $this->__('Choose an Option...') ?></option>
    </select>
    ..//
</div>
<?php endforeach; ?>
<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>

与捆绑包不同,可配置的产品选择/选项通过 javascript(在 js/varien/configurable.js 中)注入到页面中。然后这个类依赖getJsonConfig() 提供之后的所有信息。

此时,我似乎应该能够从该对象中获取一个简单的产品图片 url 信息。尽管我在configurable.js 中根本看不到处理图像的逻辑。 我将如何获取这些 url 并将它们与相关选项相关联?

任何关于我应该如何进行的建议都会很棒。

干杯


... 还有:这看起来确实是绝对必要的功能。为什么 magento 不支持这种开箱即用的东西?


更新

对于那些对类似事物感兴趣的人,我终于弄明白了。

我处理此问题的第一种方法 - 从子产品中获取图像在一定程度上起作用,但在尝试为多组属性获取唯一图像 url 时变得令人费解。这也意味着必须为简单产品中的每个选项提供图像,因此 - 大量不必要的劳动。

我最终使用了@Greg Demetrick 建议的free plugin(进行了一些修改)。它是一个非常可靠的小模块,它允许将图像 url 与任何属性选项 / 以及一些获取它们的方法相关联。

我的最终解决方案有点像这样:

catalog/product/view/type/options/configurable.phtml

<?php foreach($_attributes as $_attribute): ?>

  // markup for the attribute

    <script type="text/javascript">
        //<![CDATA[
        var attribute_data_<?php echo $_attribute->getAttributeId() ?> = {};
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.id = '<?php echo $_attribute->getAttributeId() ?>';
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.title = '<?php echo $_attribute->getLabel() ?>';
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.data = {};
        <?php 
        $a = Mage::getModel('eav/config')->getAttribute('catalog_product', $_attribute->getAttributeId() );
        $helper = Mage::helper('attributeoptionimage');
        foreach ( $a->getSource()->getAllOptions(true) as $option): ?>
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.data.option_<?php echo $option['value'] ?> = {};
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.data.option_<?php echo $option['value'] ?>.val = '<?php echo $option['value'] ?>';
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.data.option_<?php echo $option['value'] ?>.imageurl = '<?php echo $helper->getAttributeOptionImage($option['value']); ?>';
            attribute_data_<?php echo $_attribute->getAttributeId() ?>.data.option_<?php echo $option['value'] ?>.imgtitle = '<?php echo $option['label']; ?>';
        <?php endforeach; ?>
        //]]>
    </script>

    <?php endforeach; ?>

这会打印在标记中,每个属性一组。效果很好,但返回属性的所有选项,而不仅仅是为产品选择的选项(getJsonConfig() 对象已存储的数据)。所以,然后我只需针对spConfig 测试我的attribute_data_xxx 对象,并将唯一的匹配选项发送到我的控制器脚本。

扩展 getJsonConfig 以获取属性 url 可能也会起作用...

无论如何 - 这里的关键点是将图像 URL 与属性选项本身(而不是产品)相关联。

干杯

【问题讨论】:

    标签: magento option


    【解决方案1】:

    您可以使用getUsedProducts() method 获取可配置中使用的一系列简单产品。此方法不是标准产品模型的一部分,但可以在app/code/core/Mage/Catalog/Model/Product/Type/Configurable.php 中找到,因此您需要首先使用getTypeInstance() 获取可配置产品模型。该方法接受一个参数,说明您是否希望将类型模型作为单例返回(我做过)。

    foreach ($_product->getTypeInstance(true)->getUsedProducts() as $simpleProduct) {
        // From this you should be able to get the image url
    }
    

    更新

    Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig() 创建的spConfig 中有一个options 数组,其中包含特定于该可配置选项的product_id。

    spConfig :
      attributes : {
        603 : {
          id      : 603,
          code    : part_state,
          label   : "Part State",
          options : [
            {
              id       : 648,
              label    : Harvested,
              price    : 0,
              products : [204379]   // <-- Simple Product ID
            },
            {
              id       : 647,
              label    : New,
              price    : 0,
              products : [224333]
            }]
        },
      ...
    

    此时,您可以:

    1. 扩展 getJsonConfig 以包含简单的产品图片 URL,或
    2. 创建简单产品 ID 到图片 URL 的映射

    我会给你一个#2的例子,这样你就可以看到你可能会使用哪些功能。

    $spImages = array();
    foreach ($this->getAllowProducts() as $_sp) {
        $spImages[$_sp->getId()] = 
            (string)$this->helper('catalog/image')
                ->init($_sp, 'small_image')
                ->resize(40,40);
    }
    // It is necessary to cast the URL to a `string` because the actual work done by
    // Mage_Catalog_Helper_Image happens in the `__toString()` method... weird!
    
    <script type="text/javascript">
        var spImages = <?php echo json_encode($spImages); ?>;
    </script>
    

    现在您可以将图片 URL 与简单的产品 ID 关联起来,您需要根据当前选择的选项更新图片。 Magento 的Product.Config 有一个configureElement() 方法,当&lt;select class="super-attribute-select"&gt; 更改时会触发该方法,所以我会利用它。如果您不习惯这样做,您可以在 spConfig.configspImages 中获得所有信息,您可以从中编写自己的 onChange 事件处理程序。

    【讨论】:

    • 好的 - 我知道这是怎么回事......但是 - 当我将它粘贴到 cat/prod/view/options/configurable.phtml 时出现错误。具体来说,它在configurable.php 中抛出“不能在非对象上调用 getData()”(我已经尝试了几种针对该产品的方法)。假设这只是一个语法问题,那么从父级获取所有子产品是一个好的开始。我不确定我离目标更近了。最终,我需要为每个选择选项/简单产品表示捕获图像 url,就像我在链接到的捆绑产品的帖子中所做的那样。需要详细说明吗?
    • @Bosworth99 $_product 应该替换我最初发布的代码中的$product。我还更新了我的答案,专门处理将产品图像与可配置选项绑定。
    • 不错的努力。你赢了 ;) 我喜欢扩展 getJSONConfig 的想法,并且可能会走这条路。您的第二个选项也可以,我对捆绑产品做了类似的事情(但是,正如我在问题中指出的那样,它们的构建方式不同,并且出现了这个问题)。我还发现了这个块$_product-&gt;getMediaGalleryImages()-&gt;getItemByColumnValue('label', 'LABEL_NAME')-&gt;getUrl();,它在该例程中可能很有用(当有多个选项集时,识别目标属性)。无论如何。这应该让我去。干杯
    • 哦-我确实尝试过$_product-但仍然出现该错误。不知道。
    • @Bosworth99 将 getUsedProducts() 更改为 getUsedProducts(null,$_product)
    【解决方案2】:

    我认为您将在这里遇到的最终问题是属性中的选项没有与之关联的图像。您可能需要安装诸如 GoMage 的高级导航之类的东西,它将为您添加该功能。在基本 Magento 中,图像仅与产品相关联,这就是为什么您可以将它们提取为捆绑产品的原因。

    一旦你有了图片选项,你应该能够从 Magento 中添加的表格中按产品提取选项图片,在加载时显示每个选项的基本图片,然后为另一个插入 JS 交换图像上的选择。我的建议是下载下面的免费“带有选项的图像”模块并安装它。这将添加一个新的帮助程序类来显示图像,并且可能还会为您提供交换所需的代码。

    GoMage 模块:http://www.gomage.com/extensions/searching-optimization/gomage-advanced-navigation.html/

    类似功能的免费版:http://www.johannreinke.com/en/2012/02/05/magento-add-images-to-product-attribute-options/

    【讨论】:

    • 因为我可以将简单的产品与这些属性相关联(如您所知,可配置产品需要这些属性) - 似乎我应该能够根据主要产品 ID 及其属性查询这些产品。这就是我正在寻找的信息。也许这是不可能的......在这种情况下,您提到的路线可能足以作为一个很好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2011-04-14
    • 2013-04-06
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    相关资源
    最近更新 更多