0

好的,这让我很难过..

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

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

这是目前的工作代码:

$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 个选项之一。

任何帮助都会很棒。

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

4

1 回答 1

0
$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

于 2012-09-06T16:17:29.057 回答