/home/hdwebsolution/www/sareenn/application/models/Product_model.php
<?php

defined('BASEPATH') or exit('No direct script access allowed');



class Product_model extends CI_Model

{



    public function __construct()

    {

        $this->load->database();
    }

    public function get_all_product_details($id)
    {
        $this->db->select('p.*, c.category_name, c.category_slug, c.category_image, c.category_is_featured, c.category_is_active');
        $this->db->from('products p');
        $this->db->join('categories c', 'p.category_id = c.id', 'left');
        $this->db->where('p.id', $id);
        $this->db->group_by('p.id');
        $query = $this->db->get();
        return $query->row();
    }

    public function get_product_variant_parent($product_id)
    {
        $this->db->select('pv.*, p.product_name');
        $this->db->from('product_variants pv');
        $this->db->join('products p', 'pv.product_parent_id = p.id', 'left');
        $this->db->where('pv.product_id', $product_id);
        $this->db->group_by('pv.id');
        $query = $this->db->get();
        return $query->row();
    }

    public function get_product_variants($product_id, $parent_id)
    {
        $this->db->select('pv.*, p.product_name, p.product_slug');
        $this->db->from('product_variants pv');
        $this->db->join('products p', 'pv.product_id = p.id', 'left');
        $this->db->where('pv.product_parent_id', $parent_id);
        $this->db->or_where('pv.product_parent_id', $product_id);
        $this->db->order_by('pv.product_variant_order');
        $this->db->group_by('pv.id');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function total_active_products()
    {
        return $this->db->where('product_is_active', 1)->count_all_results('products');
    }

    public function get_home_products($limit = null, $offset = NULL)
    {
        $this->db->select('*');
        $this->db->where('product_is_active', '1');
        $this->db->limit($limit, $offset);
        $this->db->order_by('product_name', 'ASC');
        $this->db->from('products');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_active_best_selling_limited_products($limit)
    {
        $this->db->select('*');
        $this->db->where('product_is_active', '1');
        $this->db->where('product_is_best_selling', '1');
        $this->db->limit($limit);
        $this->db->order_by('rand()');
        $this->db->from('products');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_all_category_products_novariants($category_id)
    {
        $this->db->select('p.*, c.category_name, c.category_slug, c.category_image, c.category_is_featured, c.category_is_active, pv.product_id, pv.product_variant_name,pv.product_parent_id');
        $this->db->from('products p');
        $this->db->join('categories c', 'p.category_id = c.id', 'left');
        $this->db->join('product_variants pv', 'pv.product_id = p.id', 'left');
        $this->db->where('p.category_id', $category_id);
        $this->db->where('p.product_is_active', '1');
        $this->db->group_by('p.id');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_related_products($category_id, $product_id)
    {
        $this->db->select('*');
        $this->db->where('product_is_active', '1');
        $this->db->where('category_id', $category_id);
        $this->db->where('id !=', $product_id);
        $this->db->order_by('rand()');
        $this->db->from('products');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function main_search($keyword)
    {
        $this->db->select('p.*');
        $this->db->from('products p');
        $this->db->join('categories c', 'p.category_id = c.id', 'left');
        $this->db->where('p.product_is_active', '1');
        $this->db->like('c.category_name', $keyword);
        $this->db->or_like('p.product_name', $keyword);
        $this->db->or_like('p.product_tags', $keyword);
        $this->db->group_by('p.id');
        $this->db->order_by('p.id', 'ASC');
        $query = $this->db->get();
        return $query->result_array();
    }
}