商城配送方式实现最优算法
2010-08-27 12:10:10.0
如果商城中有好几个快递运送方式...
如何能实现最优算法呢 ??先解释下所谓的最优算法...
1.只显示能配送到该地区的快递公司
2.在上面的基础上默认选中最便宜的快递公司
3.在前两条的基础上选中最快的快递公司
需求已经明确我们来看看magento中onepage中实现shipping method的地方,
一顿乱点..发现magento默认就帮我们实现了第一条需求
我现在暂且没有去抠magento是如何实现的,我们继续完成我们的第二条需求.找到
design/fronted/xxx/template/checkout/onepage/shipping_method/available.phtml
<?php if (!($_shippingRateGroups = $this->getShippingRates())): ?>
<p><?php echo $this->__('Sorry, no quotes are available for this order at this time.') ?></p>
<?php else: ?>
<dl class="sp-methods">
<?php $min=array();?>
<?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
<dt><?php echo $this->getCarrierName($code) ?></dt>
<dd>
<ul>
<?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
<li>
<?php if ($_rate->getErrorMessage()): ?>
<ul class="messages"><li class="error-msg"><ul><li><?php echo $_rate->getErrorMessage() ?></li></ul></li></ul>
<?php else: ?>
<?php if ($_sole) : ?>
<span class="no-display"><input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>" checked="checked" /></span>
<?php else: ?>
<input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
<?php endif; ?>
<label for="s_method_<?php echo $_rate->getCode() ?>"><?php echo $_rate->getMethodTitle() ?>
<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true);
$min['s_method_'.$_rate->getCode()]=$_rate->getPrice();
?>
<?php echo $_excl; ?>
<?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
(<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
<?php endif; ?>
</label>
<?php endif ?>
</li>
<?php endforeach; ?>
</ul>
</dd>
<?php endforeach; ?><?php
foreach ($min as $key=>$val) if (empty($minval) || $val<$minval) $minval=$val;
foreach ($min as $key=>$val) if ($val==$minval){$max_arr[$key]=$val;$maxkey=$key;} ;
?>
<script type="text/javascript">
jQuery('<?php echo"#".$maxkey?>').attr("checked",true)
</script>
</dl>
<?php endif; ?>
大家直接复制过去就好了,我再来解释下如何实现默认选中最便宜的快递方式
首先我们定义一个空的数组..$min然后在
<?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
这个循环中加入
$min['s_method_'.$_rate->getCode()]=$_rate->getPrice();
这个数组是用每个快递方式的input的id值作为数组的key,用邮费作为值
然后我们再求出数组中最小的值就OK了
foreach ($min as $key=>$val) if (empty($minval) || $val<$minval) $minval=$val;
foreach ($min as $key=>$val) if ($val==$minval){$max_arr[$key]=$val;$maxkey=$key;} ;
最后还是通过jQuery的方式来默认选中我们的单选框
<script type="text/javascript">
jQuery('<?php echo"#".$maxkey?>').attr("checked",true)
</script>
测试..效果出来了,收工
文章作者:POPO4J
本文地址:http://www.popo4j.com/magento/magento_cheapest_shipping_method.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!