php - 如何根据opencart中的类别对购物车页面上的产品进行分组?

标签 php mysql opencart

我们希望根据类别获取购物车页面中的数据,以便根据编号对总金额应用折扣。同一类别的产品。

在模型文件中

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/

相关文章:

php - 从 laravel/php 中的 mysql 列数组值中查找单个值

php - 如何使用授权 header PHP

php - 解压在Linux上运行使用-d并一起覆盖

php - 如何将变量中的每个数字增加 5?

mysql - 在组的子组中查找唯一的字符串匹配

php - Opencart过滤模块查询语句、价格及特价范围

magento - Opencart,从产品的规范 URL 中删除类别字符串

php - Mysql从两个具有相同结构的不同表中选择

php - 如何在多个for循环中处理PDO对象

php - 登录后Opencart登录到上一个查看的页面?