dedecms当前栏目ID调用

dedecms系统调用当前栏目ID

{dede:type}[field:ID /]{/dede:type}

应用实例:
栏目页调用RSS地址
代码如下:

<a href="{dede:global.cfg_basehost/}/data/rss/{dede:type}[field:ID /]{/dede:type}.xml">RSS</a>
Posted in php | Tagged | Leave a comment

dedecms 文章页面添加本文链接

dedecms添加类似转载请注明本文地址之类的链接,也许对seo有一定帮助
只需要在文章模板添加代码:

转载请添加本文链接:<a href="{dede:global.cfg_basehost/}{dede:field name='arcurl' /}">{dede:global.cfg_basehost/}{dede:field name='arcurl' /}</a>
Posted in php | Tagged | Leave a comment

ecmall 品牌列表添加商品数

功能需求:品牌列表添加该品牌商品数
访问路径:index.php?app=brand
效果图:
ecmall 商品品牌 商品数目
实现代码:
修改/app/brand.app.php
添加新函数

    /**
     *
     * 品牌包含商品个数
     * @param $brand_name 品牌名
     * @return 商品数
     * @author http://www.heui.org
     */
    function _get_goods_count($brand_name) {
    	$goods_mod =& m('goods');
    	return count($goods_mod->find(array(
    		'conditions'    => 'brand ="'.$brand_name.'"'
    	)));
    }

修改推荐品牌函数:

    function _recommended_brands($num)
    {
        $brand_mod =& m('brand');
        $brands = $brand_mod->find(array(
            'conditions' => 'recommended = 1 AND if_show = 1',
            'order' => 'sort_order',
            'limit' => '0,' . $num));
        foreach ($brands as $k => $v) {
        	$brands[$k]['count'] = $this->_get_goods_count($v['brand_name']);
        }
        return $brands;
    }

所有品牌代码段修改:

        $brands_sort = array();
        foreach ($brands as $key => $val)
        {
            $brands_sort[$key] = $val['count'];
        }
        arsort($brands_sort);
        foreach ($brands_sort as $key => $val)
        {
            $brands_sort[$key] = $brands[$key];
            foreach ($brands_sort[$key]['brands'] as $k => $v) {
            	$brands_sort[$key]['brands'][$k]['count'] = $this->_get_goods_count($v['brand_name']);
            }
        }
        $this->assign('brands', $brands_sort);

模板调用:
使用count字段即可

Posted in php | Tagged | Leave a comment

ecmall 产品添加市场价

1.修改ecmall两个数据表,添加market_price字段

ALTER TABLE `ecm_goods` ADD `market_price` DECIMAL( 10,2 ) NOT NULL DEFAULT '0' AFTER `price` ;
ALTER TABLE `ecm_goods_spec` ADD `market_price` DECIMAL( 10,2 ) NOT NULL DEFAULT '0' AFTER `price` ;

2.需要修改/app/my_goods.app.php的几个地方

        switch ($goods['spec_qty'])
        {
            case 0: // 没有规格
                $specs[intval($_POST['spec_id'])] = array(
                    'market_price' => $this->_filter_price($_POST['market_price']),
                    'price' => $this->_filter_price($_POST['price']),
                    'stock' => intval($_POST['stock']),
                    'sku'      => trim($_POST['sku']),
                    'spec_id'  => trim($_POST['spec_id']),
                );
                break;
            case 1: // 一个规格
                $goods['spec_name_1'] = $spec_name_1 ? $spec_name_1 : $spec_name_2;
                $goods['spec_name_2'] = '';
                $spec_data = $spec_name_1 ? $_POST['spec_1'] : $_POST['spec_2'];
                foreach ($spec_data as $key => $spec_1)
                {
                    $spec_1 = trim($spec_1);
                    if ($spec_1)
                    {
                        if (($spec_id = intval($_POST['spec_id'][$key]))) // 已有规格ID的
                        {
                            $specs[$key] = array(
                                'spec_id' => $spec_id,
                                'spec_1' => $spec_1,
                                'market_price'  => $this->_filter_price($_POST['market_price'][$key]),
                                'price'  => $this->_filter_price($_POST['price'][$key]),
                                'stock'  => intval($_POST['stock'][$key]),
                                'sku'       => trim($_POST['sku'][$key]),
                            );
                        }
                        else  // 新增的规格
                        {
                            $specs[$key] = array(
                                'spec_1' => $spec_1,
                                'market_price'  => $this->_filter_price($_POST['market_price'][$key]),
                                'price'  => $this->_filter_price($_POST['price'][$key]),
                                'stock'  => intval($_POST['stock'][$key]),
                                'sku'       => trim($_POST['sku'][$key]),
                            );
                        }

                    }
                }
                break;
            case 2: // 二个规格
                $goods['spec_name_1'] = $spec_name_1;
                $goods['spec_name_2'] = $spec_name_2;
                foreach ($_POST['spec_1'] as $key => $spec_1)
                {
                    $spec_1 = trim($spec_1);
                    $spec_2 = trim($_POST['spec_2'][$key]);
                    if ($spec_1 && $spec_2)
                    {
                        if (($spec_id = intval($_POST['spec_id'][$key]))) // 已有规格ID的
                        {
                            $specs[$key] = array(
                                'spec_id'   => $spec_id,
                                'spec_1'    => $spec_1,
                                'spec_2'    => $spec_2,
                                'market_price'     => $this->_filter_price($_POST['market_price'][$key]),
                                'price'     => $this->_filter_price($_POST['price'][$key]),
                                'stock'     => intval($_POST['stock'][$key]),
                                'sku'       => trim($_POST['sku'][$key]),
                            );
                        }
                        else // 新增的规格
                        {
                            $specs[$key] = array(
                                'spec_1'    => $spec_1,
                                'spec_2'    => $spec_2,
                                'market_price'     => $this->_filter_price($_POST['market_price'][$key]),
                                'price'     => $this->_filter_price($_POST['price'][$key]),
                                'stock'     => intval($_POST['stock'][$key]),
                                'sku'       => trim($_POST['sku'][$key]),
                            );
                        }

                    }
                }
                break;
            default:
                break;
        }
 if (empty($default_spec))
            {
                $default_spec = array('default_spec' => $spec_id, 'market_price' => $spec['market_price'], 'price' => $spec['price']);
            }
 $default_spec = array(); // 更新商品中默认规格的信息
            foreach ($data as $key => $val)
            {
                if (empty($default_spec))
                {
                    $default_spec = array('price' => $val['price'], 'market_price' => $val['market_price']);
                }
                $this->_spec_mod->edit($key, $val);
            }
            $this->_goods_mod->edit($id, $default_spec);
 function save_spec($spec)
   {
        $data = array();
        if (empty($spec['price']) || empty($spec['stock']))
        {
            return $data;
        }
   		foreach ($spec['market_price'] as $key => $val)
        {
            $data[$key]['market_price'] = $this->_filter_price($val);
        }
        foreach ($spec['price'] as $key => $val)
        {
            $data[$key]['price'] = $this->_filter_price($val);
        }
        foreach ($spec['stock'] as $key => $val)
        {
            $data[$key]['stock'] = intval($val);
        }
        return $data;
   }

3.修改提交表单

themes/mall/default/my_goods.form.html

<li>
<h2  ectype="no_spec">市场价: </h2>
<div class="arrange"  ectype="no_spec"><input name="spec_id" value="{$goods._specs.0.spec_id}" type="hidden" /><input name="market_price" value="{$goods._specs.0.market_price}" type="text" class="text width_short" /></div>
</li>
<li>
<h2  ectype="no_spec">{$lang.price}: </h2>
<div class="arrange"  ectype="no_spec"><input name="spec_id" value="{$goods._specs.0.spec_id}" type="hidden" /><input name="price" value="{$goods._specs.0.price}" type="text" class="text width_short" /></div>
</li>

添加规格弹出窗口html修改

<ul class="th">
<li><input col="spec_name_1" type="text" class="text width4" /></li>
<li><input col="spec_name_2" type="text" class="text width4" /></li>
<li class="distance1">市场价</li>
<li class="distance1">{$lang.price}</li>
<li class="distance1">{$lang.stock}</li>
<li class="distance2">{$lang.sku}</li>
<li class="distance3">{$lang.handle}</li>
</ul>
<ul class="td" ectype="spec_item">
<li><input item="spec_1" type="text" class="text width4" /></li>
<li><input item="spec_2" type="text" class="text width4" /></li>
<li><input item="market_price" type="text" class="text width4" /></li>
<li><input item="price" type="text" class="text width4" /></li>
<li><input item="stock" type="text" class="text width4" /></li>
<li><input item="sku" type="text" class="text width8" /><input item="spec_id" type="hidden" /></li>
<li class="padding3">
<span ectype="up_spec_item" class="up_btn"></span>
<span ectype="down_spec_item" class="down_btn"></span>
<span ectype="drop_spec_item" class="delete_btn"></span>
</li>
</ul>

规格结果显示html修改

<table ectype="spec_result">
<tr>
<th col="spec_name_1">loading..</th>
<th col="spec_name_2">loading..</th>
<th col="price">市场价</th>
<th col="price">{$lang.price}</th>
<th col="stock">{$lang.stock}</th>
<th col="sku">{$lang.sku}</th>
</tr>
<tr ectype="spec_item" style="display:none">
<td item="spec_1"></td>
<td item="spec_2"></td>
<td item="market_price"></td>
<td item="price"></td>
<td item="stock"></td>
<td item="sku"></td>
</tr>
</table>

js需要修改的地方

/includes/libraries/javascript/goodsinfo.js

/* spec对象 */
function spec(id, spec1, spec2, price, market_price,stock)
{
    this.id    = id;
    this.spec1 = spec1;
    this.spec2 = spec2;
    this.price = price;
    this.market_price = market_price;
    this.stock = stock;
}

/includes/libraries/javascript/my_goods.js

var spec_1 = SPEC.spec_name_1 ? $.trim($(this).find('*[item="spec_1"]').val()) : null;
            var spec_2 = SPEC.spec_name_2 ? $.trim($(this).find('*[item="spec_2"]').val()) : null;
            var market_price = $.trim($(this).find('*[item="market_price"]').val());
            var price = $.trim($(this).find('*[item="price"]').val());
            var stock = $.trim($(this).find('*[item="stock"]').val());
        tpl.find('*[item="market_price"]').append('<input type="hidden" name="market_price['+ item.spec_id +']" value="' + item.market_price + '" />' + item.market_price);
        tpl.find('*[item="price"]').append('<input type="hidden" name="price['+ item.spec_id +']" value="' + item.price + '" />' + item.price);
        tpl.find('*[item="stock"]').append('<input type="hidden" name="stock['+ item.spec_id +']" value="' + item.stock + '" />' + item.stock);
        tpl.find('*[item="sku"]').append('<input type="hidden" name="sku['+ item.spec_id +']" value="' + item.sku + '" /><input type="hidden" name="spec_id['+ item.spec_id +']" value="' + item.spec_id + '" />' + item.sku);
        tpl.show();
        d_spec_item.before(tpl);
 spec_item && $.each(spec_item,function(i,item){ // 遍历生成规格项
        var tpl = d_spec_item.clone(true); // 克隆一个规格项
        tpl.attr('ectype', 'data'); // 赋值一个ectype与规格项模板区别
        item.spec_1 && tpl.find('*[item="spec_1"]').val(item.spec_1);
        item.spec_2 && tpl.find('*[item="spec_2"]').val(item.spec_2);
        tpl.find('*[item="market_price"]').val(item.market_price);
        tpl.find('*[item="price"]').val(item.price);
        tpl.find('*[item="stock"]').val(item.stock);
        tpl.find('*[item="sku"]').val(item.sku);
        tpl.find('*[item="spec_id"]').val(item.spec_id);
        tpl.show();
        d_spec_item.before(tpl); // 将克隆的规格项放到模板前面,新增的规格项能按正序排列
    });

修改下规格编辑窗口的宽度

/themes/mall/default/styles/default/css/user.css


.add_spec ul {
    overflow: hidden;
    width: 600px;
}

最终效果:
未添加规格时

开启规格时

Posted in php | Tagged | Leave a comment

ecshop调用dedecms文章列表

功能需求:在ecshop系统产品页面调用dedecms中的文章列表

系统:dedecms v5.7 + ecshop v2.7.2
实现步骤:
1.
在文章模版goods.dwt添加一个新的库文件dede_articles.lbi
themes/default/goods.dwt

 <!-- #BeginLibraryItem "/library/dede_articles.lbi" --><!-- #EndLibraryItem -->

2.
themes/default/library/dede_articles.lbi

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- {if $dede_articles} -->
<div class="h3Title"><h3>相关资讯</h3></div>
<ul class="brandList">
  <!-- {foreach from=$dede_articles item=article} -->
        <li><a href="{$article.url}"  title="{$article.title}" rel="external">{$article.short_title|escape:html}

</a>
        </li>
  <!-- {/foreach} -->
</ul>
<!-- {/if} -->

3.
goods.php载入dede,请根据你的目录结构修改

define('IN_ECS', true);
/*载入dede*/
require_once(dirname(__FILE__)."/luxury/include/common.inc.php");

4.
goods.php调用dede数据

$smarty->assign('dede_articles', get_dede_articles()); //dede文章列表

5.get_dede_articles函数简单实现

/**
 *
 * 获取dedecms的文章
 * @author http://www.heui.org
 * @return 文章列表
 */

function get_dede_articles() {	

	//文档排序的方式
	$orderby = 'rand';
	$ordersql = '';
	if($orderby=='hot' || $orderby=='click') $ordersql = " ORDER BY arc.click $orderWay";
	else if($orderby == 'sortrank' || $orderby=='pubdate') $ordersql = " ORDER BY arc.sortrank $orderWay";
	else if($orderby == 'id') $ordersql = "  ORDER BY arc.id $orderWay";
	else if($orderby == 'near') $ordersql = " ORDER BY ABS(arc.id - ".$arcid.")";
	else if($orderby == 'lastpost') $ordersql = "  ORDER BY arc.lastpost $orderWay";
	else if($orderby == 'scores') $ordersql = "  ORDER BY arc.scores $orderWay";
	else if($orderby == 'rand') $ordersql = "  ORDER BY rand()";
	else $ordersql = " ORDER BY arc.sortrank $orderWay";

	//limit条件
	$line = 10;
	$limit = trim(preg_replace('#limit#is', '', $limit));
	if($limit!='') $limitsql = " LIMIT $limit ";
	else $limitsql = " LIMIT 0,$line ";

	$orwhere = '';
	if(isset($orwheres[0])) {
		$orwhere = join(' And ',$orwheres);
		$orwhere = preg_replace("#^ And#is", '', $orwhere);
		$orwhere = preg_replace("#And[ ]{1,}And#is", 'And ', $orwhere);
	}
	if($orwhere!='') $orwhere = " WHERE $orwhere ";

	$addfieldsSql = '';
	$addfieldsSqlJoin = '';

	$sql =  "SELECT 

arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,tp.namerule2,tp.ispart,
            tp.moresite,tp.siteurl,tp.sitepath
            $addfieldsSql
             FROM `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
             $addfieldsSqlJoin
             $orwhere $ordersql $limitsql";

    $res = $GLOBALS['db']->query($sql);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['url']         = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
                $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row

['moresite'],$row['siteurl'],$row['sitepath']);

        $row['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
            sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];

        $arr[] = $row;
    }

    return $arr;

}

总结:最主要是需要实现获取文章路径, 在这个案例中我们载入dede, 直接调用GetFileUrl

Posted in php | Tagged , | 2 Comments