/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get group attributes $item->get_attributes = static::lazy_load(function () use($item) { $attributes = Model_Attribute_To_Groups::find(function ($query) use($item) { return $query->where('group_id', $item->id); }, 'attribute_id'); if (!empty($attributes)) { $attributes = '(' . implode(',', array_keys($attributes)) . ')'; return Model_Attribute::find(function ($query) use($attributes) { return $query->where('id', 'IN', \DB::expr($attributes))->order_by('sort', 'asc'); }, 'id'); } return array(); }, $item->id, 'attributes'); } } } // return the result return $result; }
public function action_sort_attr_in_group($type = false) { //if(!$type) return false; var_dump($items); $items = \Input::post('sort'); var_dump($items); exit; if (is_array($items)) { foreach ($items as $item) { list($item, $old_item) = explode('_', $item); if (is_numeric($item)) { $sort[] = $item; } if (is_numeric($old_item)) { $old_sort[] = $old_item; } } if (is_array($sort)) { // Get starting point for sort $start = min($old_sort); $start = $start > 0 ? --$start : $start; $model = Model_Attribute::factory(ucfirst($type)); foreach ($sort as $key => $id) { $item = $model::find_one_by_id($id); $item->set(array('sort' => ++$start)); $item->save(); } \Messages::success('Items successfully reordered.'); echo \Messages::display('left', false); } } }
public function reset_default($id = false) { // Reset default $default = Model_Attribute::find(array('where' => array('product_id' => $id, 'default' => 1))); if ($default) { foreach ($default as $item) { $item->default = 0; $item->save(); } } }
/** * 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; }
/** * 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 all product attributes $product_attributes = Model_Product::get_product_attributes($product); //var_dump($product_attributes); exit; // 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); } 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['retail_price'] = $attr_obj->retail_price; if (!empty($attr_obj->sale_price[0]->price)) { $out['sale'] = $attr_obj->sale_price[0]->price; $out['price'] = $attr_obj->sale_price[0]->price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $attr_obj->retail_price; $out['price_type'] = 'retail_price'; } } } elseif (isset($product->attributes[0]) && $product->attributes[0]->attribute_group_id == 0) { $p = $product->attributes[0]; $out['current_attributes'] = null; $out['code'] = $p->product_code; $out['images'] = $p->images; $out['retail_price'] = $p->retail_price; if (!empty($p->sale_price[0]->price)) { $out['sale'] = $p->sale_price[0]->price; $out['price'] = $p->sale_price[0]->price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $p->retail_price; $out['price_type'] = 'retail_price'; } } // 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 action_list($category_id = false, $update_category = false) { $stock_options = \Config::load('stock-option.db'); \View::set_global('title', 'Stock Control'); \View::set_global('update_category', $update_category); // Update if (\Input::post() && isset($_POST['update_stock'])) { $post = \Input::post(); $return_to = is_numeric(\Input::post('return_to')) ? '#' . \Input::post('return_to') : ''; try { foreach ($post['stock_quantity'] as $key => $value) { $attr = Model_Attribute::find_one_by_id($key); $attr->set(array('stock_quantity' => $value)); $attr->save(); } \Messages::success('Stock quantity successfully updated.'); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update stock quantity.</strong>'); // Uncomment lines below to show database errors // $errors = $e->getMessage(); // \Messages::error($errors); \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to); } \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to); } $search = $this->get_search_items($category_id); $items = $search['items']; $group = $search['group']; $pagination = $search['pagination']; $status = $search['status']; \Theme::instance()->set_partial('content', $this->view_dir . 'list')->set('items', $items)->set('group', $group)->set('pagination', $pagination, false)->set('status', $status)->set('options', $stock_options); }