PHP购物车源代码分享,如何作商城购物车
作者:admin | 时间:2018-07-17 17:20:41

对于商城类网站,购物车是不可少的功能,方便用户批量购买产品,巅云建站系统也增加了购物车模块,当然在开发原理与普通网站的购物车是一样的:

功能如下:

1、任意产品详情页可直接购买或加入购物车

加入购物车

2、可清空购物车。册除单个产品。

3、收银台结算。填写收货地址默认调用会员信息。

4、在线支付结算定单,批量修改产品定单信息和财务信息。当然巅云是多用户系统,还得批量计算各等级会员扣费及价格。

填写收货信息功能购物车在线支付

购物车代码:


1、前台下单源码:


<?session_start();require_once("../common/init.php");?><table width="255"  border="0" cellspacing="0" cellpadding="0"><tr><td width="130" rowspan="6"><div align="center"><?php        if(trim($info['tupian']==""))     {       echo "暂无图片";     }     else     {?><img src="<?php echo $info['tupian'];?>" width="130" height="100" border="0"><?php  }?></div></td>  <td width="20" height="16"> </td>  <td width="113"><font color="EF9C3E">【<?php echo $info['mingcheng'];?>】</font></td> </tr> <tr>  <td height="16"> </td>  <td><font color="910800">【市场价:<?php echo $info['shichangjia'];?>】</font></td> </tr> <tr>  <td height="16"> </td>  <td><font color="DD4679">【会员价:<?php echo $info['huiyuanjia'];?>】</font></td> </tr> <tr>  <td height="16"> </td>  <td>【<a href="lookinfo.php?IDProduct=<?php echo $info['IDProduct'];?>">查看信息</a>】</td> </tr> <tr>  <td height="16"> </td>  <td>【<a href="addgouwuche.php?IDProduct=167">放入购物车</a>】</td> </tr> <tr>  <td height="16"> </td>  <td><font color="13589B">【剩余数量:      <?php        if(($info['shuliang']-$info['cishu'])>0)      {         echo ($info['shuliang']-$info['cishu']);      }      else      {         echo "已售完";      }      ?>】</font></td> </tr> </table>

2、加入购物车代码



<?session_start();require_once("../common/init.php");require_once("../user/security.inc.php");$web=get_web();  $IDProduct=strval($_GET['IDProduct']);$sql="select * from  products where IDProduct='".$IDProduct."' and  user='$web'";   $info=$db->getdata($sql);if(($info['shuliang']-$info['cishu'])<=0) {    echo "<script>alert('该商品已经售完!');history.back();</script>";   exit;  }  $array=explode("@",$_SESSION['producelist']);  for($i=0;$i<count($array)-1;$i++)    {  if($array[$i]==$IDProduct)   {      echo "<script>alert('该商品已经在您的购物车中!');history.back();</script>";    header("location:gouwu1.php");   exit;   } }  $_SESSION['producelist']=$_SESSION['producelist'].$IDProduct."@";  $_SESSION['quatity']=$_SESSION['quatity']."1@";  header("location:gouwu1.php");?>


<?session_start();require_once("../common/init.php");require_once("../user/security.inc.php");$web=get_web();?><!DOCTYPE HTML><html><head><style>body,button,input,select,textarea{line-height:150%;font-family:'微软雅黑';font-size:14px; }body{background:#fff}select {height:32px}.red {color:#f00 !important}a{text-decoration:none;color:#3366ff}.csstable {border:1px solid #ccc;margin-bottom:10px;line-height:220%;border-right:1px solid #eee;border-bottom:1px solid #eee;}.csstable td{border-bottom:1px solid #ccc;border-right:1px solid #ccc;padding:6px 5px 3px 5px;}.csstable1 {border:01px solid #ccc;margin-bottom:10px;line-height:220%;border-right:1px solid #eee;border-bottom:1px solid #eee;}.csstable1 td{border-bottom:0px solid #ccc;border-right:0px solid #ccc;padding:3px 5px 3px 5px;}input{border:1px solid #aaa;background:#fff;height:30px;text-align:center}.buttoncss{background:#c00;color:#fff;border:0;padding:3px 30px}</style></head><body><div class="part1  pad bgf yahei" style="padding:0px 20px">	<h3 class="font26" style="padding-bottom:10px;border-bottom:0px solid #ccc;"><span class="l title"></span><span class="r more"></span></h3>	<div class="bx-c">		<div class="content">      <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="csstable">        <form name="form1" method="post" action="gouwu1.php">              <?php     $_SESSION["total"];     if($_GET['qk']=="yes")     {        $_SESSION['producelist']="";     $_SESSION['quatity']="";       }      $arraygwc=explode("@",$_SESSION['producelist']);      $s=0;      for($i=0;$i<count($arraygwc);$i++)      {          $s+=intval($arraygwc[$i]);      }     if($s==0 )       {       echo "<tr>";   echo" <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>";   echo"</tr>";    }     else      {     ?><tr style="font-weight:bold">  <td width="125" height="25" bgcolor="#FFFFFF"><div align="center">商品名称</div></td>  <td width="52" bgcolor="#FFFFFF"><div align="center">数量</div></td>  <td width="64" bgcolor="#FFFFFF"><div align="center">市场价</div></td>  <td width="64" bgcolor="#FFFFFF"><div align="center">会员价</div></td>  <td width="51" bgcolor="#FFFFFF"><div align="center">折扣</div></td>  <td width="66" bgcolor="#FFFFFF"><div align="center">小计</div></td>  <td width="71" bgcolor="#FFFFFF"><div align="center">操作</div></td></tr><?php/** * 购物车 商品数量管理*/$total=0;$array=explode("@",$_SESSION['producelist']);$arrayquatity=explode("@",$_SESSION['quatity']);//var_dump($array);     while(list($name,$value)=each($_POST))        {          for($i=0;$i<count($array)-1;$i++)          {             if(($array[$i])==$name)                {                  $arrayquatity[$i]=$value;                    }            }             }    $_SESSION['quatity']=implode("@",$arrayquatity);    for($i=0;$i<count($array)-1;$i++)     {         $id=$array[$i];       $num=$arrayquatity[$i];      if($id!="")       {      $sql="select * from  products where IDProduct='".$id."' and  user='$web'";  	    $info=$db->getdata($sql);$pics = json_decode($info['pics'], true);   if (isset($pics)) {                                        foreach ($pics as $key=>$v) {																				$product_pic=$pics[0];																				}}$PInfo=PInfo($id);$Price=$PInfo['iPrice'];       $total1=$num*$Price;       $total+=$total1;       $_SESSION["total"]=$total;   ?><tr>  <td height="25" width='150' bgcolor="#FFFFFF"><div align="center"><?php if ($product_pic==""){?><img src="/skin/default/../skin/v7red//skin/moban0_idc/resources/no_pic.png" style="width:60px;height:40px;" /> <?php }else{?><img src="<?=$product_pic?>" style="width:60px;height:40px;" /><?php }?><br><?php echo $info['ProductName'];?></div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center">      <input type="text" name="<?php echo $info['IDProduct'];?>" size="2" class="inputcss" value=<?php echo $num;?>>  </div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $PInfo['oPrice'];?>元</div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $PInfo['iPrice'];?>元</div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo @(ceil(($PInfo['iPrice']/$PInfo['oPrice'])*100))."%";?></div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $PInfo['iPrice']*$num."元";?></div></td>  <td height="25" bgcolor="#FFFFFF"><div align="center"><a href="removegwc.php?IDProduct=<?php echo $info['IDProduct']?>">移除</a></div></td></tr><?php                   }     }?><tr>  <td height="35" colspan="8" bgcolor="#FFFFFF"><div align="right">      <table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0" style="border:0" class="csstable1">        <tr>          <td width="125"><div align="center"> <input type="submit" value="更改商品数量" class="buttoncss">          </div></td>          <td width="125"><div align="center"><a href="gouwu2.php">去收银台</a></div></td>          <td width="125"><div align="center"><a href="gouwu1.php?qk=yes">清空购物车</a></div></td>          <td width="195"><div align="left" style="color:red;font-size:24px">总计:<?php echo $total;?></div></td>        </tr>      </table> <?php    }   ?>	</div>	</div>	<div class="bx-b"><div class="bx-b2"></div></div></div></div></body></html>

3、清空购物车单个商品


<?session_start();require_once("../common/init.php");$web=get_web();if($_SESSION['User_S']=="")  {    echo "<script>alert('请先登录,后购物!');history.back();</script>"; exit;  }   $IDProduct=$_GET['IDProduct'];$array=explode("@",$_SESSION['producelist']);$arrayquatity=explode("@",$_SESSION['quatity']);//var_dump($array);           for($i=0;$i<count($array)-1;$i++)          {             if(($array[$i])==$IDProduct)                {                                    unset($arrayquatity[$i]);				   unset($array[$i]);                 }            }             $_SESSION['quatity']=implode("@",$arrayquatity);    $_SESSION['producelist']=implode("@",$array);   die("<script>history.back()</script>");   ?>

4、填写收货信息。

<?session_start();require_once("../common/init.php");require_once("../user/security.inc.php");$web=get_web();$sql="select * from users where UserName='".$_SESSION['User_S']."' and user='$web'";  $uinfo=$db->getdata($sql);?><!DOCTYPE HTML><html><head><style>a{text-decoration:none;color:#3399ff}body{background:#fff}select {height:32px}.red {color:#f00 !important}body,button,input,select,textarea{line-height:150%;font-family:'微软雅黑';font-size:14px; }.csstable {border:1px solid #ccc;margin-bottom:10px;line-height:220%;border-right:1px solid #eee;border-bottom:1px solid #eee;}.csstable td{border-bottom:1px solid #ccc;border-right:1px solid #ccc;padding:6px 5px 3px 5px;}.csstable .bar{font-weight:bold;background:url(images/bar.png);}.csstable .s{background:#eee;}input{border:1px solid #aaa;background:#fff;height:30px;}.buttoncss{background:#c00;color:#fff;border:0;padding:0px 30px;height:30px;}.buttoncss1{background:#ccc;color:#fff;border:0;padding:0px 10px;height:30px;width:40px;}</style></head><body><div class="part1  pad bgf yahei" style="padding:0px 20px">	<h3 class="font26" style="padding-bottom:2px;border-bottom:0px solid #ccc;"><span class="l title">  收货人信息</span><span class="r more"></span></h3>	<div class="bx-c">		<div class="content">      <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="csstable">              <script language="javascript">/** * 购物车 收货人信息 * Edit www.jb51.net*/function chkinput(form)    {      if(form.name.value=="")       {      alert("请输入收货人姓名!");      form.name.select();      return(false);    }    if(form.dz.value=="")       {      alert("请输入收货人地址!");      form.dz.select();      return(false);    }    if(form.yb.value=="")       {      alert("请输入收货人邮编!");      form.yb.select();      return(false);    }    if(form.tel.value=="")       {      alert("请输入收货人联系电话!");      form.tel.select();      return(false);    }    if(form.email.value=="")       {      alert("请输入收货人E-mail地址!");      form.email.select();      return(false);    }    if(form.email.value.indexOf("@")<0)     {        alert("收货人E-mail地址格式输入错误!");        form.email.select();        return(false);     }    return(true);         }           </script>     <form name="form1" method="post" action="savedd.php" onSubmit="return chkinput(this)">      <tr> <td width="100" height="25" bgcolor="#FFFFFF"><div align="center">收货人姓名:</div></td> <td width="183" bgcolor="#FFFFFF"><div align="left"><input type="text" name="Contact_GB" size="25" value="<?=$uinfo['Contact_GB']?>" class="inputcss" ></div></td> <td width="86" bgcolor="#FFFFFF"><div align="center">性别:</div></td> <td width="176" bgcolor="#FFFFFF"><div align="left"> <select name="sex">  <option selected value="男">男</option>  <option value="女">女</option> </select> </div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">详细地址:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input name="dz" type="text" name="Addr_GB" value="<?=$uinfo['Province_GB']?><?=$uinfo['City_GB']?><?=$uinfo['Addr_GB']?>" class="inputcss" id="dz"  size="25"> </div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">邮政编码:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="PostCode" size="25" value="<?=$uinfo['PostCode']?>" class="inputcss" ></div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">联系电话:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="Tel" size="25" class="inputcss" value="<?=$uinfo['Tel']?>"></div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">电子邮箱:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="Email" size="25" class="inputcss"  value="<?=$uinfo['Email']?>" ></div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">送货方式:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"> <select name="shff" id="shff">  <option selected value="虚拟发货">虚拟发货</option>  <option  value="普通平邮">普通平邮</option>  <option value="特快专递">特快专递</option>  <option value="送货上门">送货上门</option>   <option value="E-mail">E-mail</option>      </select></div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">支付方式:</div></td> <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"> <select name="zfff" id="zfff">   <option selected value="网上支付">网上支付</option>  <option  value="银行汇款">银行汇款</option> </select> </div></td> </tr> <tr> <td height="100" bgcolor="#FFFFFF"><div align="center">简单留言:</div></td> <td height="100" colspan="3" bgcolor="#FFFFFF"><div align="left"> <textarea name="ly" cols="60" rows="4" class="inputcss" style="background-color:#fff;border:1px solid #ccc " ></textarea></div></td> </tr> <tr style=""> <td height="45" colspan="4" bgcolor="#FFFFFF"><div align="center"><input type="submit"  value="提交订单" class="button yahei bg4 buttoncss"> <a href="gouwu1.php" > [Back]</a> </div></td> </tr>   </form>           </tr></table>	</div>	</div>	<div class="bx-b"><div class="bx-b2"></div></div></div></div></body></html><!--<?php if($_GET['dingdanhao']!="")  {  $dd=$_GET['dingdanhao'];        $array=explode("@",$_SESSION['producelist']);  $sum=count($array)*20+260;    echo" <script language='javascript'>"; echo" window.open('showdd.php?dd='+'".$dd."','newframe','top=150,left=200,width=600,height=".$sum.",menubar=no,toolbar=no,location=no,scrollbars=no,status=no ')"; echo "</script>";  }?>-->

4、确认定单,并写入数据库,定单表和产品购买表双记录修改。


<?php
session_start();
require_once("../common/init.php");
require_once("../user/security.inc.php");
$web=get_web();
$sqls="select * from users where UserName='$_SESSION[User_S]' and user='$web'";
$info=$db->getdata($sqls);
$fIDProd=$_SESSION['producelist'];


$Num=$_SESSION['quatity'];


 $arrayNum=explode("@",$_SESSION['quatity']);
      $Num=0;
      for($i=0;$i<count($arrayNum);$i++)
      {
          $Num+=intval($arrayNum[$i]);
      }






$Contact_GB=$_POST['Contact_GB'];
$sex=$_POST['sex'];
$Addr_GB=$_POST['Addr_GB'];
$PostCode=$_POST['PostCode'];
$Tel=$_POST['Tel'];
$Email=$_POST['Email'];
$wuliu=$_POST['shff'];
$payment=$_POST['zfff'];
if(trim($_POST['ly'])=="")
{
$About="";
}
else
{
 $About=$_POST['ly'];
}
$UserName=$_SESSION['User_S'];
$dtCreate=date("Y-m-j H:i:s");
 //$dtExpiry=DATE_ADD(now(),INTERVAL $iYear YEAR)//SQL代码
$iStatus=0;
$Amount=$_SESSION["total"];
$array=explode("@",$_SESSION['producelist']);
$arrayquatity=explode("@",$_SESSION['quatity']);
//var_dump($array);
     $IDHost=array();
          for($i=0;$i<count($array)-1;$i++)
          {
             


$sql="insert into host set UserName='$_SESSION[User_S]',fIDProd='{$array[$i]}',user='$web',Num={$arrayquatity[$i]},".
     
"dtCreate=now(),iStatus='0',dtExpiry=DATE_ADD(now(),INTERVAL $arrayquatity[$i] YEAR)";
$db->query($sql);
$IDHost[$i]=$db->insert_id();
          
                 
            }   

   $IDHosts=implode("|",$IDHost);


$IDProduct=implode("|",explode("@",$_SESSION['producelist']));
 $IDProduct=substr( $IDProduct, 0, -1); 






$oOrder = Order::AddOrderPL($IDProduct,$IDHosts,0,$About,1,$Num,"",1,$Amount,$Contact_GB,$sex,$Addr_GB,$PostCode,$Tel,$Email,$wuliu,$payment);
$display=($_CFG[PayOnline]==1)?"":"none";


$OrderID=$oOrder['OrderID'];
header("location:/user/onlinepay.php?Amount={$Amount}&OrderID=$OrderID");      
?>
5、后台定单管理。


文章内容