Reputation: 23
We want to fetch data in cart page according to category to apply discount on total amount according to no. of products of same category.
In model file
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;
}
}
}
In controller file /catalog/controller/checkout/cart.php
$r[]=$this->model_discount_cdiscount->getCategory($product['product_id']);
Upvotes: 2
Views: 1797
Reputation: 531
////////////// 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']
);
}
-
Upvotes: 1
Reputation: 16055
Your model function is wrong.
First of all, $this->db->query($sql)
returns an object with properties row
, rows
and num_rows
. Therefore Your first foreach
loop is wrong.
Second, if You select category ids from product_to_ctegory
for one concrete product, You always have to expect array of category ids, therefore You'd have to stick with $query->rows
property. And the second foreach
loop has completely no sense to me...
So this should be Your method:
public function getCategories($product_id) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = " . (int)$product_id);
$categories = array();
foreach($query->rows as $row) {
$categories[] = $row['category_id'];
}
echo "Product ({$product_id}) is linked to categories with IDs: " . implode(', ', $categories);
}
Notice also the SQL query variable typecasting - to avoid SQL injection...
Though I am not sure what do You want to achieve by loading all the categories for a product, I believe I got You on the right way. Anyway, it may be more usable for You if You only select the main (1st level) categories (with parent_id === 0
), so the query might look like:
$query = $this->db->query("SELECT c.category_id FROM " . DB_PREFIX . "product_to_category ptc LEFT JOIN " . DB_PREFIX . "category c ON c.category_id = ptc.category_id WHERE ptc.product_id = " . (int)$product_id . " AND c.parent_id = 0");
This, of course, requires that all the products are properly linked (nested) to the categories - from the top to the bottom levels (I have seen many live systems, where proucts were linked only to the bottom level categories...).
Upvotes: 1