【问题标题】:PHP/MYSQL Making a shopping cart coupon code work for one of two productsPHP/MYSQL 使购物车优惠券代码适用于两种产品之一
【发布时间】:2012-09-06 15:14:21
【问题描述】:

好吧,这让我很难过..

我目前有一个系统,其中购物车检查优惠券表以查看购物者输入的优惠券代码是否对产品有效。

这很好用,但我希望扩展系统,以便将单个代码用于多个产品之一。

下面是工作代码:

$coupon = mysql_real_escape_string(htmlentities(strtoupper($_POST['coupon'])));
$coup_qry = mysql_query("select tblcoupon.*, sell_product.product_code,sell_product.id from tblcoupon,sell_product WHERE tblcoupon.code='$coupon' AND sell_product.product_code = tblcoupon.products");
if($numrows = mysql_num_rows($coup_qry) > 0) // check if typed coupon exists in tblcoupon table 
{
    while($coup_w = mysql_fetch_array($coup_qry))           
    {
        $start_date = $coup_w['start_date'];
        $end_date = $coup_w['end_date'];
        $start = strtotime($start_date);
        $todays_date = date("Y-m-d");
        $today = strtotime($todays_date);
        $expiration_date = strtotime($end_date);
        if($today >= $start && $today <= $expiration_date) // if todays date is > start date 
                                                            and < end date then execute the below script                
        {   
            $coupon_productcode = $coup_w['products'];    //product code in the tblcoupon table
            $sell_product_productcode = $coup_w['product_code'];
            //check if any of the products in the shopping cart match any of the products 
            foreach ($_SESSION['cart'] as $name => $value) 
            {
                $id = substr($name, 5, (strlen($name)-5));
                if($id==$coup_w['id'])
                {
                    $_SESSION['coupon'] = "C$coupon-$id";
                    if(isset($_SESSION['offer'])) { 
                        unset($_SESSION['offer']); 
                    }
                    if(isset($_SESSION['error'])) { 
                        unset($_SESSION['error']); 
                    }       
                    $errCoupon = "Coupon code accepted-green";
                } 
                else {
                    $errCoupon = "Coupon Code is Invalid for this product.";
                }
            }
        } 
        else if ($today < $start){
            $errCoupon = "Coupon Code is not yet active!-red";
        } 
        else if ($today > $expiration_date){
            $errCoupon = "Coupon Code has expired!-red";
        }
    }
}

我需要做的是在tbl_coupon 中添加一个名为products2 的新列(这将是不同的产品代码),并让SQL 查询查找sell_product.product_code = tblcoupon.products OR sell_product.product_code = tblcoupon.products2 所在的行

然后,我需要检查 $_SESSION['cart'] $id 的产品是否是有效选项(productsproducts2),但不能同时使用两者。优惠券只能用于 2 个选项之一。

任何帮助都会很棒。

如果您需要更多信息,请尽管询问。

【问题讨论】:

  • 如果我没看错,您想制作适用于多种产品但只能应用于一件商品的优惠券。这是正确的吗?

标签: php sql shopping-cart


【解决方案1】:
$coupon = mysql_real_escape_string(htmlentities(strtoupper($_POST['coupon'])));
$coup_qry = mysql_query("select tblcoupon.*, sell_product.product_code,sell_product.id from tblcoupon,sell_product WHERE tblcoupon.code='$coupon' AND sell_product.product_code = tblcoupon.products");
if($numrows = mysql_num_rows($coup_qry) > 0) // check if typed coupon exists in tblcoupon table 
    {
        while($coup_w = mysql_fetch_array($coup_qry))           
                    {
                        $coup_w['used'] = 0; //Coupon has not been used yet
                        $start_date = $coup_w['start_date'];
                        $end_date = $coup_w['end_date'];
                        $start = strtotime($start_date);
                        $todays_date = date("Y-m-d");
                        $today = strtotime($todays_date);
                        $expiration_date = strtotime($end_date);
                        if($today >= $start && $today <= $expiration_date) // if todays date is > start date and < end date then execute the below script               
                            {   
                                $coupon_productcode = $coup_w['products'];  //product code in the tblcoupon table
                                $sell_product_productcode = $coup_w['product_code'];
                                //check if any of the products in the shopping cart match any of the products 
                                foreach ($_SESSION['cart'] as $name => $value) 
                                    {
                                        $id = substr($name, 5, (strlen($name)-5));
                                        //test if the coupon matches AND has not been used
                                        if($id == $coup_w['id'] && $coup_w['used'] == 0)                                                
                                            {                                                                                                       
                                                $_SESSION['coupon'] = "C$coupon-$id";
                                                if(isset($_SESSION['offer'])) { unset($_SESSION['offer']); }                                                    
                                                if(isset($_SESSION['error'])) { unset($_SESSION['error']); }                                                    
                                                $errCoupon = "Coupon code accepted-green";
                                                $coup_w['used'] = 1; //Coupon has now been used.
                                            } 
                                        else {$errCoupon = "Coupon Code is Invalid for this product.";}                                         
                                    }
                            } 
                        else if ($today < $start){$errCoupon = "Coupon Code is not yet active!-red";} 
                        else if ($today > $expiration_date){$errCoupon = "Coupon Code has expired!-red";}
                    }

}

“仅使用一次优惠券”部分的简单解决方案是在 $coup_w 数组中携带一个“已使用”键,以指示优惠券是否已使用(如果为假,则为 0,如果为真,则为 1)。然后您需要测试优惠券ID是否匹配并且优惠券尚未使用。如果满足条件,则优惠券有效,“已使用”应设置为 1。

至于“product1/product2”,我认为您不应该限制自己的列数。我认为最好的方法是这样做:

//YOU'LL HAVE TO MAKE SURE THIS IS VALID, I'M NOT SURE, BUT THE LOGIC IS THE IDEA
"SELECT ... AND tblcoupon.products LIKE CONCAT('%', sell_product.product_code, '%')"

其中 tblcoupon.products 是以逗号分隔的优惠券适用的产品代码列表。

我还要提一下,不建议使用 mysql_ 函数。您应该查看 mysqli_ 或 PDO

【讨论】:

  • 今天应该试试看它是否正常,谢谢。可悲的是,为了一致性,我使用 mysql 很糟糕,我没有编写电子商务网站,我试图用以前的 web 开发人员编写的代码来修补缺陷..(谢天谢地,我正在努力也完全重写,但这需要更长的时间,而我令人愉快的销售团队需要在下周之前更换优惠券:p)
  • 刚刚用 CONCAT 进行了测试,推车抛出了一些未定义的错误,但这个概念有效。我有一个代码适用于目前的三种产品之一(除了错误),如果你尝试一次将它应用于所有 3 个项目,它只会打折列表中的第一个 :) 谢谢 :) 现在看看我是否可以解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 2013-11-28
  • 2011-06-21
  • 1970-01-01
  • 2017-01-11
  • 2017-09-15
相关资源
最近更新 更多