gpt4 book ai didi

magento - 选择不同的可配置产品时更改显示的图像(例如不同的颜色)

转载 作者:行者123 更新时间:2023-12-02 13:53:03 28 4
gpt4 key购买 nike

我目前正在努力获取在我的第一个 magento 版本中显示的产品选项的图像。我有this计算出捆绑产品,如下所示:

screenshot

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

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

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

在 Magento 中,我看到:

在主题/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>

与 bundle 不同,可配置产品选择/选项通过 javascript 注入(inject)到页面上(在 js/varien/configurable.js 中)。然后,此类依赖 getJsonConfig() 来提供此后的所有信息。

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

最佳答案

您可以使用 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
}

更新

spConfigMage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig() 创建有一个options包含特定于该可配置选项的product_ids 的数组。

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 到图像网址的映射

我将为您提供#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()<select class="super-attribute-select"> 时触发的方法变化,所以我会利用它。如果您不愿意这样做,您可以在 spConfig.config 中获取所有信息。和spImages您可以从中编写自己的 onChange事件处理程序。

关于magento - 选择不同的可配置产品时更改显示的图像(例如不同的颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11456819/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com