0

好吧,我试图获取每种产品的运费,但我不知道正确获取运费,例如我的 product_shipping_cost 表

   product_id, shipping_cost
   1, 12
   2, 15
   etc.....

现在我尝试使用此查询获取单个产品 shipping_cost

    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
    $cost = $query->row['shipping_cost'];

是的,我得到了完美的运费!但是如果我想获得两个或更多的运费怎么办?我的意思是将 shipping_cost 与选定的 product_id 相加,因此它将是 12+15=27 的运费,我使用它没有运气..

    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
    foreach($query->row as $shipping) {
    $cost = $shipping['shipping_cost'];
    }

有任何想法吗?

4

3 回答 3

0

通过使用此代码完成

    $total = 0;
    foreach ($this->cart->getProducts() as $product) {
        $product_id = $product['product_id'];

        $query = $this->db->query("SELECT shipping_cost FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
        $total += $query->row['shipping_cost'];
        $cost = $total;
    }

但是如果数据库中没有设置产品的 shipping_cost 仍然会返回错误

于 2013-09-14T09:41:17.783 回答
0

在查询中添加成本:

$query = $this->db->query("SELECT SUM(shipping_cost) total_shipping FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
$cost = $query->row['total_shipping'];
于 2013-09-14T06:53:23.737 回答
0

$cost 需要是一个数组来存储多个成本,除非您打算在 for 循环中使用它。IE..

$costTotal = 0;
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost");
foreach($query->row as $shipping) {
$costTotal += $shipping['shipping_cost'];
}

更新以反映操作评论。

于 2013-09-14T06:12:37.523 回答