Magento - 如何检索捆绑的选项图像 [英] Magento - how to retrieve bundled option images
问题描述
我一直致力于我的第一个 magento 部署.建立了一个非常定制的主题......现在解决一些非标准的定制:
I've been working on my first magento deploy. Got a pretty customized theme built up... tackling some of the non-standard customizations now:
我的主要产品类型之一是办公椅,我将其设置为捆绑产品.这种产品类型有很多可用选项(大约 100 种面料选项、手臂样式、腰部、头枕等),我需要能够在目录/产品/查看页面上显示每个选项的图像.
One of my primary product types is an office chair which I have set up as a bundled product. There are many options available for this product type (about 100 fabric options, arm style, lumbar, headrest etc) and I need to be able to show an image for each of these on the catalog/product/view page.
作为捆绑产品(我不会讨论这是否是正确的产品类型——我们在可配置和捆绑产品之间进行了循环辩论)——每个产品都由许多简单的产品组装而成(作为选项).这些简单的产品可以上传图片,我已经这样做了.我现在想从媒体文件夹中检索网址...
Being a bundled product (and I'll refrain from any discussion about whether this was the right product type - we went around in circles debating between a configurable and a bundled) - each product is assembled from a number of simple products (as options). These simple products can have images uploaded to them, and I've done so. I now want to retrieve the urls to those from the media folder...
经过一番探索 - 这些似乎是基本要素:
After some hunting - these seem to be the essential elements:
theme/../template/catalog/product/view/options.phtml
<?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
<dl>
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
theme/../template/bundle/catalog/product/view/type/bundle/option/select.phtml
<?php /* @var $this Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Select */ ?>
<?php $_option = $this->getOption(); ?>
<?php $_selections = $_option->getSelections(); ?>
我发现终于找到了getSelections()
:
I found finally found getSelections()
in:
class Mage_Bundle_Model_Resource_Price_Index extends Mage_Core_Model_Resource_Db_Abstract
{ ...
/**
* Retrieve bundle options with selections and prices by product
*
* @param int $productId
* @return array
*/
public function getSelections($productId)
{
$options = array();
$read = $this->_getReadAdapter();
$select = $read->select()
->from(
array('option_table' => $this->getTable('bundle/option')),
array('option_id', 'required', 'type')
)
->join(
array('selection_table' => $this->getTable('bundle/selection')),
'selection_table.option_id=option_table.option_id',
array('selection_id', 'product_id', 'selection_price_type',
'selection_price_value', 'selection_qty', 'selection_can_change_qty')
)
->join(
array('e' => $this->getTable('catalog/product')),
'e.entity_id=selection_table.product_id AND e.required_options=0',
array()
)
->where('option_table.parent_id=:product_id');
$query = $read->query($select, array('product_id' => $productId));
while ($row = $query->fetch()) {
if (!isset($options[$row['option_id']])) {
$options[$row['option_id']] = array(
'option_id' => $row['option_id'],
'required' => $row['required'],
'type' => $row['type'],
'selections' => array()
);
}
$options[$row['option_id']]['selections'][$row['selection_id']] = array(
'selection_id' => $row['selection_id'],
'product_id' => $row['product_id'],
'price_type' => $row['selection_price_type'],
'price_value' => $row['selection_price_value'],
'qty' => $row['selection_qty'],
'can_change_qty' => $row['selection_can_change_qty']
);
}
return $options;
}
所以我们得到一个包含 selection_id
、product_id
、price_type
等的数组......但没有任何内容涉及该选择的图像 url...
so we get back an array with selection_id
, product_id
, price_type
etc... but nothing referring to the image url for that selection...
鉴于:
class Mage_Catalog_Helper_Product extends Mage_Core_Helper_Url
{ ...
public function getImageUrl($product)
{
$url = false;
if (!$product->getImage()) {
$url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
}
elseif ($attribute = $product->getResource()->getAttribute('image')) {
$url = $attribute->getFrontend()->getUrl($product);
}
return $url;
}
我正在尝试构建一个 js 对象,其中包含对每个选择的图像 url 的引用,类似于 theme/../template/bundle/catalog/product/view/type/bundle/option/select.phtml
I'm trying to build a js object with refs to each selection's image url kind of like so in theme/../template/bundle/catalog/product/view/type/bundle/option/select.phtml
var option_set_<?php echo $_option->getId() ?> = {};
<?php foreach ($_selections as $_selection): ?>
option_set_<?php echo $_option->getId() ?>._<?php echo $_selection->getSelectionId() ?> = '<?php echo $_selection->getImageUrl(); ?>';
<?php endforeach; ?>
但是 $_selection
显然没有正确输入,因为它只是让我跳转到占位符图形.
But $_selection
obviously isn't typed correctly because it just bounces me to the placeholder graphic.
假设这些选项可以输入为产品(或以某种方式从 selection_id 中获取简单的产品,看起来类似的东西可以工作.我不确定这是否正是我想要的管理这个功能,但如果我能得到一堆网址 - 那么我就会做生意
Assuming these options can be typed as a Product (or somehow obtain the simple product from the selection_id, it seems like something like that can work. I'm not sure if that's exactly how I want to manage this feature, but if I can just get a stack of urls - then I'll be in business
奇怪的是,互联网基本上对这个主题保持沉默.显然没有人需要为他们的产品选项显示图像(或者,唯一的解决方案是付费扩展 - 这将是最后的手段).
Weirdly, the interwebs is basically silent on this subject. Apparently nobody needs to show images for their product options (or, rather, the only solutions are paid extensions - which will be a last resort).
我该如何解决这个问题?
How I can go about figuring this out?
耶稣踢踏舞基督.法师还能再复杂点吗?
Jesus tapdancing christ. Could Mage be any more complicated?
更新
让它这样工作:
<?php echo $this->helper('catalog/image')->init($_selection, 'small_image')->resize(40,40); ?>
如果有人需要此修复程序,我选择的答案也可以正常工作.
The answer I selected will also work fine, if anybody needs this fix.
推荐答案
$_selections 中的 product_id 表示选中的简单产品的 id.所以要做的就是:
The product_id in the $_selections represents the id of the selected simple product. So all to do is:
- 取$_selections的product_id,
- 通过这个 product_id 加载产品
- 将生成的产品对象提供给 Mage_Catalog_Helper_Product::getImageUrl.
这篇关于Magento - 如何检索捆绑的选项图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!