【发布时间】: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 与属性选项本身(而不是产品)相关联。
干杯
【问题讨论】: