我们希望根据类别获取购物车页面中的数据,以便根据编号对总金额应用折扣。同一类别的产品。
在模型文件中
public function getCategory($data)
{
$sql = "SELECT category_id FROM " . DB_PREFIX . "Product_to_category where product_id='".$data."'";
$query = $this->db->query($sql);
foreach($query as $key=>$value)
{
//print_r($key); echo "=>"; print_r($value);
foreach($value as $k=>$v)
{
//echo $k."=>".$v;
return $k."=".$v;
}
}
}
在 Controller 文件/catalog/controller/checkout/cart.php中
$r[]=$this->model_discount_cdiscount->getCategory($product['product_id']);
最佳答案
////////////// Grouping Same category products into one array //////////////
$category_id = $this->model_discount_cdiscount->getcategory($product['product_id']);
$array_key = $category_id[0]['category_id'];
if (array_key_exists($array_key, $this->data['discount']))
{
$this->data['discount'][$array_key]['total'] = $this->data['discount'][$array_key]['total']+(preg_replace("/[^0-9.]/","",$total));
$this->data['discount'][$array_key]['quantity'] = $this->data['discount'][$array_key]['quantity']+$product['quantity'];
}
else
{
$this->data['discount'][$array_key] = array(
'category_name' => $category_id[0]['name'],
'category_id' => $array_key,
'total' => (preg_replace("/[^0-9.]/","",$total)),
'quantity' => $product['quantity'],
'length' => $product['length']
);
}
-
关于php - 如何根据opencart中的类别对购物车页面上的产品进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440393/