【问题标题】:Update shipping price after picked price (PHP)选择价格后更新运费 (PHP)
【发布时间】:2012-05-11 23:16:04
【问题描述】:

我正在为我的网站制作购物车。在购物车区域,我从数据库中提取运输详细信息,包括取决于邮政编码的价格。当用户点击“更新购物车”时。 我正在尝试使用运费更新总计。如果有人能指出我解决这个问题的正确方向。

这是我展示购物车的功能。

function showCart() {
$cart = $_SESSION['cart'];
$zip = $_SESSION['zipcode'];
if ($cart) {

    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) {
        $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    }
    $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
    $output[] = '<table>';
    foreach ($contents as $id=>$qty) {
        $sql = 'SELECT * FROM products WHERE id = '.$id;
        $result = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        extract($row);
        $output[] = '<tr>';
        $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
        $output[] = '<td>'.$title.' by '.$author.'</td>';
        $output[] = '<td>$'.$price.'</td>';
        $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
        $output[] = '<td>$'.($price * $qty).'</td>';
        $total += $price * $qty;
        $output[] = '</tr>';
    }
    $output[] = '</table>';

    $sql  = 'SELECT * FROM zipcodes WHERE zipcode = '.$zip;
        $result = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

        $output[] = '<label>Price: </label><select name="delivery" id="delivery" class="long-field">';
        $output[] = '<option value="">Please Select</option>';
        $output[] = '<option value='.$row['free_dev'].'>$'.$row['free_dev'].'</option>';
        $output[] = '<option value='.$row['next_day_dev'].'>$'.$row['next_day_dev'].'</option>';
        $output[] = '</select>';
        }

    $output[] = '<p>Delivery Charge: <strong>$'.$delivery.'</strong></p>';          
    $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
    $output[] = '<div><button type="submit">Update cart</button></div>';
    $output[] = '</form>';
} else {
    $output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);

}

这是通过 cart.php 更新的部分

case 'update':
if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
        if (stristr($key,'qty')) {
            $id = str_replace('qty','',$key);
            $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if ($id != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                    }
                }
            }
            for ($i=1;$i<=$value;$i++) {
                if ($newcart != '') {
                    $newcart .= ','.$id;
                } else {
                    $newcart = $id;
                }
            }
        }
    }
}
$cart = $newcart;
break;

谢谢。任何帮助是极大的赞赏! PS我正在使用这里的购物车教程http://v3.thewatchmakerproject.com/journal/276/

【问题讨论】:

  • 那么,您在网上找到了一些代码,复制了它,现在您要求别人为您修改它?至少自己尝试一下,发布您的尝试然后寻求帮助
  • 我只是问是否有人能指出我正确的方向。
  • “当用户点击“更新购物车”时,我还想在新的总计中包含运费。我将如何使用下面的代码实现这一点?这是一个“为我写代码”的请求。
  • 对不起,保罗,我的意思并不是说我是在要求别人为我做这项工作。我将重新编辑我的请求。
  • 不用担心。当你证明你已经尝试过某事时,你会从人们那里得到更多帮助。

标签: php shopping-cart


【解决方案1】:

鉴于您提供的代码对这个问题相当不可知,因此不清楚您在哪里遇到了困难。但为了让您开始:

case 'update':
if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
        ... //same thing as before
    }
    $newcart .= $shipping_price; //or however you're encoding this. It's unclear.
}
$cart = $newcart;
break;

还有替换:

$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';

与:

$output[] = '<p>Grand total: <strong>$'.$total+ $shipping_price.'</strong></p>';

【讨论】:

    【解决方案2】:

    如果我是你,我会搜索一个 PHP 类购物车,该代码让我想起了一些 PHP4(它应该是因为示例来自 2005 年)。

    我没有找到一个很好的示例链接,我将尝试在下面描述这个想法。

    您创建了两个类,一个代表商品,另一个代表购物车(第一个可以只是来自带有 mysql_fetch_object 的数据库的结果,或者类似 Pdo 的东西,它会将其从 php 转换为 stdClass

    class someItem 
    

    { 公共$id; 公共$数量; 公开的$价格; 公共 $totalprice;

    public function __get($var)
    {
        if ($var == 'totalPrice') {
            return $this->price*$this->qty;
        }
    
        return $this->$var;
    }
    

    }

    类购物车 {

    private $items;
    
    public function addItem($someItem)
    {
        if (array_key_exists($someItem->id, $this->items)) {
            $this->items[$someItem->id]->qty = $this->items[$someItem->id]->qty + $someItem->qty;
        } else {
            $this->items[$someItem->id] = $someItem;
        }
    }
    
    public function removeItem($someItem)
    {
        unset($this->items[$someItem->id]);
    }
    
    public function getTotal()
    {
        $total = 0;
        foreach($this->items as $item){
            $total += $item->totalPrice;
        }
        return $total;
    }
    

    }

    将信息序列化到会话中 $_SESSION['user']['cart'] = serialize($Shoppingcart);

    用户登录时设置一个新的购物车,当他退出时清理它。 添加和删​​除项目可以在一切之前完成

    // 根据您的 HTML 表单进行任何您想要和处理的操作 foreach ($_POST['cart'] as $itemId ) { $ItemObj = $db->getitemAsObject($itemId); // 使用 PDO 什么的 $shoppingcart->addItem($itemObj); }

    发布后将购物车重新保存到会话 $_SESSION['user']['cart'] = serialize($Shoppingcart);

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多