商城方式实现最优算法

来源:互联网 发布:c语言double什么意思 编辑:程序博客网 时间:2024/04/28 09:37

如果商城中有好几个快递运送方式...

 

如何能实现最优算法呢 ??先解释下所谓的最优算法...

1.只显示能配送到该地区的快递公司

2.在上面的基础上默认选中最便宜的快递公司

3.在前两条的基础上选中最快的快递公司

需求已经明确我们来看看magento中onepage中实现shipping method的地方,

一顿乱点..发现magento默认就帮我们实现了第一条需求

我现在暂且没有去抠magento是如何实现的,我们继续完成我们的第二条需求.找到

view source
print?
1design/fronted/xxx/template/checkout/onepage/shipping_method/available.phtml
view source
print?
01<?php if (!($_shippingRateGroups = $this->getShippingRates())): ?>
02<p><?php echo $this->__('Sorry, no quotes are available for this order at this time.') ?></p>
03<?php else: ?>
04<dl class="sp-methods">
05<?php $min=array();?>
06<?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
07<dt><?php echo $this->getCarrierName($code) ?></dt>
08<dd>
09<ul>
10<?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
11<li>
12<?php if ($_rate->getErrorMessage()): ?>
13<ul class="messages"><li class="error-msg"><ul><li><?php echo $_rate->getErrorMessage() ?></li></ul></li></ul>
14<?php else: ?>
15<?php if ($_sole) : ?>
16<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>
17<?php else: ?>
18<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" />
19<?php endif; ?>
20<label for="s_method_<?php echo $_rate->getCode() ?>"><?php echo $_rate->getMethodTitle() ?>
21<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
22<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true);
23$min['s_method_'.$_rate->getCode()]=$_rate->getPrice();
24?>
25<?php echo $_excl; ?>
26<?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
27(<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
28<?php endif; ?>
29</label>
30<?php endif ?>
31</li>
32<?php endforeach; ?>
33</ul>
34</dd>
35<?php endforeach; ?><?php
36foreach ($min as $key=>$val) if (empty($minval) || $val<$minval) $minval=$val;
37foreach ($min as $key=>$val) if ($val==$minval){$max_arr[$key]=$val;$maxkey=$key;} ;
38?>
39<script type="text/javascript">
40jQuery('<?php echo"#".$maxkey?>').attr("checked",true)
41</script>
42</dl>
43<?php endif; ?>

大家直接复制过去就好了,我再来解释下如何实现默认选中最便宜的快递方式

首先我们定义一个空的数组..$min然后在

view source
print?
1<?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>

这个循环中加入

view source
print?
1$min['s_method_'.$_rate->getCode()]=$_rate->getPrice();

这个数组是用每个快递方式的input的id值作为数组的key,用邮费作为值

然后我们再求出数组中最小的值就OK了

view source
print?
1foreach ($min as $key=>$val) if (empty($minval) || $val<$minval) $minval=$val;
2foreach ($min as $key=>$val) if ($val==$minval){$max_arr[$key]=$val;$maxkey=$key;} ;

最后还是通过jQuery的方式来默认选中我们的单选框

view source
print?
1<script type="text/javascript">
2jQuery('<?php echo"#".$maxkey?>').attr("checked",true)
3</script>

测试..效果出来了,收工

原创粉丝点击