public function get_search_items($category_id = 0) { // Override category_id if its a search $category_id = \Input::get('category_id', $category_id); // Reset $parent_id if its invalid value is_numeric($category_id) or $category_id = 0; if ($category_id) { $category = \Product\Model_Category::find_one_by_id($category_id); if ($category) { \View::set_global('category', $category); } } /************ Start generating query ***********/ $items = Model_Category::find(function ($query) use($category_id) { // Select only root pages $query->where('parent_id', $category_id); // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'active_from': $date = strtotime($value); if ($date) { $query->where($key, '>=', $date); } break; case 'active_to': $date = strtotime($value); if ($date) { $query->where($key, '<=', $date); } break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); /************ End generating query ***********/ // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); return array('items' => $items, 'pagination' => $pagination, 'status' => $status); }
/** * Get product data (code, price, attributes, images) * * @access public * @param $product object * @return */ public static function product_data($product = false, $attributes_json = null, $post_select = false, $attributeid = false) { if (!$product) { return; } $out['current_attributes'] = null; $out['code'] = null; $out['images'] = null; $out['select'] = null; $out['select_name'] = null; $out['sx'] = null; // select (attribute) order $out['retail_price'] = null; $out['sale'] = null; $out['price'] = 0; $out['price_type'] = null; $current_attributes = array(); $select = array(); $select_name = array(); $sx = array(); $options = array(); if (json_decode($attributes_json) != null) { $current_attributes = Model_Attribute::find(array('where' => array('attributes' => $attributes_json, 'product_id' => $product->id, 'active' => 1), 'limit' => 1)); } //get attributes per product $prod_attributes = \Product\Model_Attribute::find_by_product_id($product->id); $a_attribute_ids = array(); if ($prod_attributes) { foreach ($prod_attributes as $attr_id) { array_push($a_attribute_ids, $attr_id->id); } } // NRB-Gem: if current URL is Pricing Group -> Products // Get pricing_attribute_price from current Pricing Group // Products can belong to multiple Pricing Groups $a_groups = \Sentry::user()->groups(); if ($a_attribute_ids) { if (\Sentry::check()) { $a_cond = array(array('product_attribute_id', 'in', $a_attribute_ids)); if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && $a_groups[0]) { $a_cond['user_group_id'] = $a_groups[0]['id']; } if (\Uri::segment(3) == 'group' && \Uri::segment(4) == 'products') { $a_cond['pricing_group_id'] = \Uri::segment(5); } $product_sale_price = \Product\Model_Attribute_Price::find(array('where' => $a_cond)); } } // NRB-Gem: check if logged in user can buy product for discounted rate if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && !\Sentry::user()->is_admin() && $a_groups[0]) { $o_product_group_options = \DB::select_array(array('able_to_buy'))->from('product_group_options')->join('product_to_groups')->on('product_group_options.product_group_id', '=', 'product_to_groups.group_id')->where('product_group_options.user_group_id', $a_groups[0]['id'])->where('able_to_buy', 1)->where('product_to_groups.product_id', $product->id)->execute(); $a_result = $o_product_group_options->as_array(); $product_sale_price = $a_result && $a_result[0]['able_to_buy'] ? $product_sale_price : false; } // Get all product attributes $product_attributes = Model_Product::get_product_attributes($product); // Product with attributes if ($product_attributes) { // Set current attribute if ($current_attributes && isset($product_attributes[$current_attributes[0]->id])) { $current_attributes = $product_attributes[$current_attributes[0]->id]; } elseif ($post_select && count($post_select) > 1) { foreach ($post_select as $sk => $sv) { if (!isset($post_select_pop)) { $post_select_pop = $post_select; array_pop($post_select_pop); } $count = count($post_select_pop); foreach ($product_attributes as $pk => $pv) { $i = 0; $not_exists = array(); foreach ($pv as $pkk => $pvv) { if ($i >= $count) { continue; } if (!isset($post_select_pop[$pvv->attribute->id]) || $post_select_pop[$pvv->attribute->id] != $pvv->option->id) { $not_exists[] = true; } $i++; } if (empty($not_exists)) { $current_attributes = $product_attributes[$pk]; break 2; } } if (empty($current_attributes) && $count > 1) { array_pop($post_select_pop); } } if (empty($current_attributes)) { $current_attributes = reset($product_attributes); } } elseif ($product->default_attributes) { $current_attributes = $product_attributes[reset($product->default_attributes)->id]; } else { $current_attributes = reset($product_attributes); } foreach ($current_attributes as $k => $v) { $options[] = $v->option->id; } foreach ($product_attributes as $k => $v) { if (is_array($v)) { foreach ($v as $kk => $vv) { $select_name[$vv->attribute->id] = $vv->attribute->name != '' ? $vv->attribute->name : $vv->attribute->title; $select_tmp[$k][$vv->attribute->id][$vv->option->id] = $vv->option->title; foreach ($options as $option_key => $option_value) { if ($kk == $option_key) { $select[$vv->attribute->id][$vv->option->id] = $vv->option->title; if ($vv->option->id != $option_value) { continue 3; } } } } } } if ($select) { $sx = array_keys($select_name); } $b_use_original_price = false; if (!empty($current_attributes)) { $attr_obj = $current_attributes[0]->product_attribute; $out['current_attributes'] = $current_attributes; $out['code'] = $attr_obj->product_code; if (!empty($attr_obj->images)) { $out['images'] = $attr_obj->images; } $out['select'] = $select; $out['select_name'] = $select_name; $out['sx'] = $sx; $out['stock_quantity'] = $attr_obj->stock_quantity; $out['retail_price'] = $attr_obj->retail_price; // NRB-Gem: changed condition from: (!empty($product_sale_price) && $product_sale_price[0]->price != 0) // fix for attributes with sale price are not displayed if (!empty($product_sale_price)) { $out['with_attr'] = array('attribute' => array()); foreach ($product_sale_price as $with_attr) { $out['with_attr']['able_discount'][$with_attr->product_attribute_id] = $with_attr->able_discount; if ($out['with_attr']['able_discount'][$with_attr->product_attribute_id]) { $out['with_attr']['discount'][$with_attr->product_attribute_id] = $with_attr->discount; $out['with_attr']['price'][$with_attr->product_attribute_id] = $with_attr->price; } $out['with_attr']['attribute'][$with_attr->product_attribute_id] = ''; foreach ($product_attributes as $o_prod_attr) { $a_attr = array(); foreach ($o_prod_attr as $o_attr) { if ($with_attr->product_attribute_id == $o_attr->product_attribute->id) { $a_attr[] = $o_attr->option->title; } } $out['with_attr']['attribute'][$with_attr->product_attribute_id] .= implode(' | ', $a_attr); } // NRB-Gem: do not display inactive attribute if (empty($out['with_attr']['attribute'][$with_attr->product_attribute_id])) { unset($out['with_attr']['attribute'][$with_attr->product_attribute_id]); unset($out['with_attr']['able_discount'][$with_attr->product_attribute_id]); unset($out['with_attr']['discount'][$with_attr->product_attribute_id]); unset($out['with_attr']['price'][$with_attr->product_attribute_id]); } } // NRB-Gem: get default prices foreach ($prod_attributes as $attr) { if (isset($out['with_attr']['discount'][$attr->id]) && $out['with_attr']['discount'][$attr->id] > 0) { $out['with_attr']['price'][$attr->id] = $attr->retail_price - $attr->retail_price * ($out['with_attr']['discount'][$attr->id] / 100); } else { if (isset($out['with_attr']['price'][$attr->id]) && !$out['with_attr']['price'][$attr->id]) { $out['with_attr']['price'][$attr->id] = $attr->sale_price ? $attr->sale_price : $attr->retail_price; } } } $out['sale'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->discount); $out['price'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->price); $out['price_type'] = 'sale_price'; // NRB-Gem: Set discounted rate from product_attribute_price Product attributes if ($attributes_json) { foreach ($current_attributes as $o_attribute) { if ($o_attribute->product_attribute->attributes == $attributes_json) { if ($out['with_attr']['able_discount'][$o_attribute->product_attribute->id]) { if ($out['with_attr']['discount'][$o_attribute->product_attribute->id] > 0) { $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$o_attribute->product_attribute->id] / 100); } else { $out['price'] = $out['with_attr']['price'][$o_attribute->product_attribute->id]; } } else { $b_use_original_price = true; } } } } } else { $out['price'] = \Product\Model_Product::calculate_discount($product, $attr_obj->retail_price); $out['price_type'] = 'retail_price'; $b_use_original_price = true; } // NRB-Gem: Display original price if Discount price is not enabled (used by non-ajax request) if (\Uri::segment(1) == 'product' && !$attributes_json) { $a_prod_attr_keys = array_keys($product_attributes); $attr_id = $a_prod_attr_keys[0]; if (isset($out['with_attr']['able_discount'][$attr_id])) { if (isset($out['with_attr']['discount']) && $out['with_attr']['discount'][$attr_id] > 0) { $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$attr_id] / 100); } else { $out['price'] = isset($out['with_attr']['price']) ? $out['with_attr']['price'][$attr_id] : ''; } } else { $b_use_original_price = true; $attributes_json = true; } } // NRB-Gem: Set original discounted rate for Product attributes if ($b_use_original_price && $attributes_json) { if ($attr_obj->sale_price > 0) { $out['price'] = $attr_obj->sale_price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $attr_obj->retail_price; unset($out['price_type']); } } } } else { $p = $product->attributes[0]; $out['current_attributes'] = null; $out['code'] = $p->product_code; $out['images'] = $p->images; $out['retail_price'] = $p->retail_price; $out['stock_quantity'] = $p->stock_quantity; $out['able_discount'] = isset($product_sale_price[0]) ? $product_sale_price[0]->able_discount : 0; if (!empty($product_sale_price) && $product_sale_price[0]->price != 0) { if ($out['able_discount']) { $out['sale'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->discount); $out['price'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->price); } $out['price_type'] = 'sale_price'; } else { $out['sale'] = isset($product_sale_price[0]) ? $product_sale_price[0]->discount : 0; $out['price'] = \Product\Model_Product::calculate_discount($product, $p->retail_price); $out['price_type'] = 'retail_price'; } // NRB-Gem: Display original price if Discount price is not enabled if ($out['able_discount']) { if ($product_sale_price[0]->discount > 0) { $out['price'] = $p->retail_price - $p->retail_price * ($product_sale_price[0]->discount / 100); } } else { if ($p->sale_price > 0) { $out['price'] = $p->sale_price; $out['price_type'] = 'sale_price'; } else { unset($out['price_type']); $out['price'] = $p->retail_price; } } } $prod_category = Model_Product_To_Categories::find_by_product_id($product->id); $category = \Product\Model_Category::find_one_by_id($prod_category[0]->category_id); $out['category_id'] = $prod_category ? $prod_category[0]->category_id : null; $out['category'] = $category ? $category->title : null; // Product without attributes if (empty($out['current_attributes'])) { $out['code'] = $product->code; } // Set images from product if there is no images in product attribute if (empty($out['images']) && !empty($product->images)) { $out['images'] = $product->images; } return $out; }
public function get_search_items($category_id = false) { // Override category_id if its a search $category_id = \Input::get('category_id', $category_id); $product_ids = array(); // If we are viewing category products // We need to find all products from that and child categories if (is_numeric($category_id)) { $category = \Product\Model_Category::find_one_by_id($category_id); if ($category) { \View::set_global('category', $category); if ($category->all_products) { $product_ids = array_keys($category->all_products); } } } // If we are filtering products by category and there is nothing found // We return empty array of products without even going to database if (empty($product_ids) && is_numeric($category_id)) { $items = array(); } else { /************ Start generating query ***********/ $items = Model_Product::find(function ($query) use($product_ids) { if (!empty($product_ids)) { $query->where('id', 'in', $product_ids); } // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%")->or_where('code', 'like', "%{$value}%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'active_from': $date = strtotime($value); if ($date) { $query->where($key, '>=', $date); } break; case 'active_to': $date = strtotime($value); if ($date) { $query->where($key, '<=', $date); } break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); } /************ End generating query ***********/ // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); return array('items' => $items, 'pagination' => $pagination, 'status' => $status); }
public function get_search_products($category_id = false) { // Override category_id if its a search $category_id = \Input::get('category_id', $category_id); $product_ids = array(); // If we are viewing category products // We need to find all products from that and child categories if (\Input::get()) { if (\Input::get('status') != 'select') { if (is_numeric($category_id)) { $category = \Product\Model_Category::find_one_by_id($category_id); if ($category) { \View::set_global('category', $category); if ($category->all_products) { $product_ids = array_keys($category->all_products); } } } } } // If we are filtering products by category and there is nothing found // We return empty array of products without even going to database if (empty($product_ids) && is_numeric($category_id)) { $items = array(); } else { /************ Start generating query ***********/ if (\Input::get()) { if (\Input::get('status') != 'select') { $items = \Product\Model_Product::find(function ($query) use($product_ids) { if (!empty($product_ids)) { $query->where('id', 'in', $product_ids); } // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%")->or_where('code', 'like', "%{$value}%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'active_from': $date = strtotime($value); if ($date) { $query->where($key, '>=', $date); } break; case 'active_to': $date = strtotime($value); if ($date) { $query->where($key, '<=', $date); } break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); } } } /************ End generating query ***********/ // Reset to empty array if there are no result found by query if (empty($items)) { $items = array(); } return array('items' => $items); }