首页 > cms建站系统 > ecshop

Ecshop系统添加免运费赠品后购物车变为收取运费解决办法

发布时间:2016-11-25 17:29

使用商城的朋友可能会经常碰到关于利用ecshop来实现一些活动的需求,把电影票设置为免运费。但是事实情况却事与愿违,消费者购买订单超过100元并添加免运费的赠品后,订单反而又要收取邮费

flow.php

 代码如下 复制代码

/* 添加赠品到购物车 */
foreach ($favourable['gift'] as $gift)
{
    if (in_array($gift['id'], $_POST['gift']))
    {
        add_gift_to_cart($act_id, $gift['id'], $gift['price']);
    }
}

出问题的地方正是函数“add_gift_to_cart”,找到函数的定义,仍在在flow.php文件,

我把该函数修改后的源码贴出来,大家对照一下就明白了:

 代码如下 复制代码

function add_gift_to_cart($act_id, $id, $price)
{
    $sql = "INSERT INTO " . $GLOBALS['ecs']->table('cart') . " (" .
                "user_id, session_id, goods_id, goods_sn, goods_name, market_price, goods_price, ".
                "goods_number, is_real, extension_code, parent_id, is_gift, rec_type, is_shipping ) ".
            "SELECT '$_SESSION[user_id]', '" . SESS_ID . "', goods_id, goods_sn, goods_name,

market_price, ".
                "'$price', 1, is_real, extension_code, 0, '$act_id', '" . CART_GENERAL_GOODS . "' ,

is_shipping " .
            "FROM " . $GLOBALS['ecs']->table('goods') .
            " WHERE goods_id = '$id'";
    $GLOBALS['db']->query($sql);
}

这样就很好的解决了Ecshop系统添加免运费赠品后,购物车变为收取运费的bug这个问题了。

本文章完美的利用了php的curl功能实现模拟登录discuz以及模拟发帖,本教程供参考学习哦。
 代码如下 复制代码

<?php

$discuz_url = ‘http://localhost/klive/root/Discuz_X2.5_SC_GBK/’;//论坛地址
$login_url = $discuz_url .’member.php?mod=logging&action=login’;//登录页地址

$post_fields = array();
//以下两项不需要修改
$post_fields['loginfield'] = ‘username’;
$post_fields['loginsubmit'] = ‘true’;
//用户名和密码,必须填写
$post_fields['username'] = ‘admin’;
$post_fields['password'] = ‘admin’;
//安全提问
$post_fields['questionid'] = 0;
$post_fields['answer'] = ”;
//@todo验证码
$post_fields['seccodeverify'] = ”;

//获取表单FORMHASH
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*value=”(.*?)”s*/>/i’, $contents,

$matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die(‘Not found the forumhash.’);
}

//POST数据,获取COOKIE,cookie文件放在网站的temp目录下
$cookie_file = tempnam(‘./temp’,'cookie’);

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);

//取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
$send_url = $discuz_url.”forum.php?mod=post&action=newthread&fid=2″;

$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*id=”formhash”s*value=”(.*?)”s*/>/i

’, $contents, $matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die(‘Not found the forumhash.’);
}

$post_data = array();
//帖子标题
$post_data['subject'] = ‘test2′;
//帖子内容
$post_data['message'] = ‘test2′;
$post_data['topicsubmit'] = “yes”;
$post_data['extra'] = ”;
//帖子标签
$post_data['tags'] = ‘test’;
//帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
$post_data['formhash']=$formhash;

$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装REFERER
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$contents = curl_exec($ch);
curl_close($ch);

//清理cookie文件
unlink($cookie_file);

?>

如果利用php来判断就简单了,原理很简单:

识别是否是动画,要检查文件中是否包含chr(0×21).chr(0xff).chr(0x0b).’NETSCAPE2.0‘chr(0×21).chr(0xff)

是gif图片中扩展功能段的标头‘NETSCAPE2.0‘是扩展功能执行的程序名


代码如下。

 代码如下 复制代码

<?php 
function IsAnimatedGif($filename) 

    $fp = fopen($filename, 'rb'); 
    $filecontent = fread($fp, filesize($filename)); 
    fclose($fp); 
   return strpos($filecontent,chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0') === FALSE?0:1; 

echo IsAnimatedGif("test.gif"); 
?>

在php中简单数据搜索很简单我们直接使用in_array() 函数在数组中搜索给定的值即可,这种是简单的一维数据

 代码如下 复制代码

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn",$people))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }
?>

输出:

Match found。

array_key_exists()函数
如果在一个数组中找到一个指定的键,函数array_key_exists()返回true,否则返回false。其形式如下:


1 boolean array_key_exists(mixed key,array array);

下面的例子将在数组键中搜索apple,如果找到,将输出这个水果的颜色:

 

 代码如下 复制代码

1 $fruit["apple"] = "red"; 

2 $fruit["banana"] = "yellow"; 

3 $fruit["pear"] = "green"; 

4 if(array_key_exists("apple", $fruit)){ 

5     printf("apple's color is %s",$fruit["apple"]); 

6 }

执行这段代码得到的结果:


1 apple's color is red


array_search()函数

 代码如下 复制代码

$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$founded = array_search("green", $fruits);
if($founded)
 printf("%s was founded on %s.",$founded, $fruits[$founded])

array_keys()函数

 代码如下 复制代码

$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$keys = array_keys($fruits);
print_r($keys);

上面的方法都只能搜索一维数据,如果是多维数据就没办法了。


1 php搜索多维数组的键值

如下面例子:

 代码如下 复制代码

$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';

如果要查找 bar 3 怎么进行查找呢。有三个结果,而这三个结果都要,看下面的函数:
-------------------------------------------------------------------------------------------------------------------------------

 代码如下 复制代码
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1=>$value1) {
    $nodes_temp[$a] = $key1;
    if (is_array($value1)){  
      array_search_re($needle, $value1, $a, $nodes_temp);
    }
    else if ($value1 === $needle){
      $nodes_found[] = $nodes_temp;
    }
}
return $nodes_found;
}

---------------------------------------------------------------------------------------------------------------------------------
这个函数就可以把上面要查找到的内容全部返回出键名来

 代码如下 复制代码

$result = array_search_re('bar 3', $foo);

print_r($result);

输出结果为如下:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
          [1] => Array ( [1] => 3 [2] => c [3] => dd )
          [2] => Array ( [1] => 3 [2] => f [3] => gg )
        )

1 php搜索多维数组的键名

 代码如下 复制代码

function array_search_key($needle, $haystack){
global $nodes_found;

foreach ($haystack as $key1=>$value1) {
 
 if ($key1=== $needle){
 
  $nodes_found[] = $value1;
      
   }
    if (is_array($value1)){  
      array_search_key($needle, $value1);
    }
  
  
}

return $nodes_found;
}
$result = array_search_key('a', $foo);

print_r($result);

输出结果为如下:
 

Array
(
    [0] => Array
        (
            [xx] => bar 1
        )

    [1] => Array
        (
            [bb] => bar 3
        )

    [2] => Array
        (
            [yy] => bar 4
        )

)

通过遍历我们可以实现多维数据搜索了。

PHP是怎么返回json格式的数据呢,json数据是一种特殊数据格式,可以实时也数据进行交互了,下面我来介绍PHP是怎么返回json格式的数据的吧。

我们常见一些网站在做ajax时返回JSON格式的数据:

返回的是json格式的数据

php输出JSON格式


显然并非所愿。还是字符串,到底怎么实现?其实很简单,只要在php文件头部加入以下代码:

 代码如下 复制代码

header('Content-type: text/json');

示例代码:

 代码如下 复制代码

< ?php
header('Content-type: text/json');

$fruits = array (
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);
echo json_encode($fruits);
?>

先来看jquery

 代码如下 复制代码


$(function(){
     $('#send').click(function() {
          $.getJSON('http://blog.meituo.net/wp-content/uploads/php_return_json/test.js', function(data) {
              $('#resText').empty();
   var html = '';
   $.each( data  , function(commentIndex, comment) {
    html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>';
   })
  $('#resText').html(html);
         })
    })
})

你需要做的就是将数据存储为格式正确的 .json或者.js 文件。以下为示例所传送的json格式的数据

 

 代码如下 复制代码
[
  {
    "username": "张三",
    "content": "沙发."
  },
  {
    "username": "李四",
    "content": "板凳."
  },
  {
    "username": "王五",
    "content": "地板."
  }
]

php 使用json_encode函数,jQuery使用datatype:json的返回类型

标签:[!--infotagslink--]

您可能感兴趣的文章: