Magento组商品显示商品属性

magento组商品显示商品属性表格
magento组商品显示商品属性表格

Magento组商品(Magento grouped product),是用来对属于一个系列或者一个组合的商品进行销售。如上图,为默认情况下的Magento组商品,此例子是对于一个沙发组合进行销售。组商品的特点就是你可以根据自己需要,每件可以购买不同的数量。

在某些时候,我们可能需要更加详细的内容显示,如下图,为销售一些五金商品,那么购买者可能需要更多的参数信息。

magento组商品显示每件商品属性
magento组商品显示每件商品属性

我们可以看到,这就是对组商品进行扩展,让其显示更多的属性值。

这个是怎样做到的呢?

首先,我们要对Magento的属性组和属性非常熟悉,见我博客商品的Magento中文视频中有详细的教程。我们给组商品建立一个属性组,属性组中就有如上图中的,Diameter inch和Diameter mm等属性,然后在模板文件中将这些属性输出出来。下图,为名字为‘type1’的属性组,红色部分为添加的属性。

magento组商品的属性组
magento组商品的属性组

我们可能有很多类组商品,并且每一类组商品的表都不一样,那么就需要为每一类组商品创建一个属性组。

然后,当我们的属性组与属性设置好后,添加一个组商品,好看下我们设置的是否正确。因为我们还没有修改模板文件,前台的现实结果还和以前一样。

下面我们就去修改模板文件,模板文件路径为:app/design/frontend/default/你的模板文件夹/template/catalog/product/view/type /grouped.phtml 代码实例如下

<!-- type1 -->
<?php if($_product->getAttributeSetId() == 11): ?>

	<table class="data-table grouped-items-table" id="super-product-table">
	<tr>
		<td rowspan="2"><?php echo $this->__('Item number') ?></td>
		<td colspan="2"><?php echo $this->__('Diameter') ?></td>
		<td ><?php echo $this->__('Hole diameter') ?></td>
		<td colspan="3"><?php echo $this->__('Segment dimension mm') ?></td>
		<td rowspan="2"><?php echo $this->__('Number of segments') ?></td>
		<td rowspan="2"><?php echo $this->__('Price') ?></td>
		<td rowspan="2"><?php echo $this->__('Qty') ?></td>
	</tr>
	<tr  class="tr-title">
		<td ><?php echo $this->__('inch') ?></td>
		<td ><?php echo $this->__('mm') ?></td>
		<td ><?php echo $this->__('mm') ?></td>
		<th><?php echo $this->__('height') ?></th>
		<th><?php echo $this->__('thickness') ?></th>
		<th><?php echo $this->__('length') ?></th>
	</tr>

	<?php if ($_hasAssociatedProducts): ?>
		<?php foreach ($_associatedProducts as $_item): ?>
			<?php $_finalPriceInclTax = $this->helper('tax')->getPrice($_item, $_item->getFinalPrice(), true) ?>
			<tr>

				<td><?php echo $this->htmlEscape($_item->getSku()) ?></td>
				<td><?php echo $_item->getData('diameter_inch'); ?></td>
				<td><?php echo $_item->getData('diameter_mm'); ?></td>
				<td><?php echo $_item->getData('hole_diameter_mm'); ?></td>
				<td><?php echo $_item->getData('segment_dimension_height'); ?></td>
				<td><?php echo $_item->getData('segment_dimension_thickness'); ?></td>
				<td><?php echo $_item->getData('segment_dimension_length'); ?></td>
				<td><?php echo $_item->getData('number_of_segments'); ?></td>
				<?php if ($this->getCanShowProductPrice($_product)): ?>
				<td class="a-right">
					<?php if ($this->getCanShowProductPrice($_item)): ?>
					<?php echo $this->getPriceHtml($_item, true) ?>
					<?php echo $this->getTierPriceHtml($_item) ?>
					<?php endif; ?>
				</td>
				<?php endif; ?>
				<?php if ($_product->isSaleable()): ?>
				<td class="a-center">
				<?php if ($_item->isSaleable()) : ?>
					<input type="text" name="super_group[<?php echo $_item->getId() ?>]" maxlength="12" value="<?php echo $_item->getQty()*1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
				<?php else: ?>
					<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
				<?php endif; ?>
				</td>
				<?php endif; ?>
			</tr>
		<?php endforeach; ?>
		<?php else: ?>
		   <tr>
			   <td colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>"><?php echo $this->__('No options of this product are available.') ?></td>
		   </tr>
	<?php endif; ?>
	</table>
<?php endif; ?>
<!-- type1 end -->

分析代码:

此代码是应对网站中有多种组商品的。由于每一个组商品我们都制定一个只属于它自己的属性组,所以我可以先通过判断属性组,知道有是哪个组商品,有哪些属性,如何显示表格。判断当前商品是哪个属性组的语句如下。

<?php if($_product->getAttributeSetId() == 12): ?>

每个属性组都有唯一的ID(后台创建完属性组,即可查),我们通过ID判断。

对当前的属性组获取其属性输出的语句示例如下

<?php echo $_item->getData('diameter_mm'); ?>

如上,‘diameter_mm’就是在后台添加的属性名字,通过属性名字获得其值。

通过html语言的组织,我们用table将页面弄的美观些,就有如上面第二张图的效果了。

 

本例代码使用magento1.5版本进行讲解,其它版本可能稍有不同。适合有一定magento开发经验的人学习参考。

 

转载表明出处:www.hellokeykey.com

 

 

《Magento组商品显示商品属性》有7个想法

  1. 钥匙, 通过此语句
    getData(‘diameter_mm’); ?>
    为什么不能获取产品组中的下拉选项(dropdown)的值. 求指点, 谢谢

    1. 可以获得,是一样的。获得设置的那个值,无论这个值是填上的还是下拉菜单选出来的,都可以用这个函数来获得,修改参数就可以了。

  2. 大神你好~~~我是最近老大让我做magento。。。没有人请教。。。好痛苦 刚好做到第二张图的要求。。。我现在只是还在用鼠标点击的阶段。。。我想问下如果想做成图二那个产品按列属性的样子 可以用鼠标点击得到吗?谢谢急求

评论已关闭。