function add_item() { global $FUNCS, $DB; if (isset($_POST['pp_id']) && $FUNCS->is_non_zero_natural($_POST['pp_id'])) { $item_number = (int) $_POST['pp_id']; $rs = $DB->select(K_TBL_PAGES, array('id', 'template_id'), "id = '" . $DB->sanitize($item_number) . "'"); if (count($rs)) { $rec = $rs[0]; $pg = new KWebpage($rec['template_id'], $rec['id']); if (!$pg->error) { // get all cart related fields from page $arr_pp_fields = array('pp_price', 'pp_options', 'pp_requires_shipping'); $arr_custom_fields = array(); for ($x = 0; $x < count($pg->fields); $x++) { if ($pg->fields[$x]->system || $pg->fields[$x]->deleted) { continue; } $fname = $pg->fields[$x]->name; if (in_array($fname, $arr_pp_fields)) { ${$fname} = trim($pg->fields[$x]->get_data()); } else { // is it a custom field? Check if prefixed with a 'pp_' if (substr($fname, 0, 3) == 'pp_') { $arr_custom_fields[substr($fname, 3)] = trim($pg->fields[$x]->get_data()); // strip off the 'pp_' prefix } } } $all_ok = 1; // valid price if (!isset($pp_price) || !is_numeric($pp_price)) { $all_ok = 0; } // valid quantity $quantity = trim($_POST['qty']); if ($this->get_config('allow_decimal_qty')) { if (!is_numeric($quantity) || !preg_match("/^[0-9.]+\$/i", $quantity) || !($quantity > 0)) { $all_ok = 0; } } else { if (!$FUNCS->is_non_zero_natural($quantity)) { $all_ok = 0; } } if ($all_ok) { $arr_sort_keys = array(); // used to sort items in the cart $arr_display_attrs = array(); // an array of all selected variant options with values $arr_sort_keys[] = $pg->page_name; //get the price modifiers, if any if (isset($pp_options)) { $arr_opts = $this->_parse_options($pp_options); if (count($arr_opts)) { for ($x = 0; $x < count($arr_opts); $x++) { $os = $_POST['os' . $x]; $opt_name = $arr_opts[$x]['name']; $opt_values = $arr_opts[$x]['values']; // valid attributes if ($this->_is_option_text($arr_opts[$x])) { // textbox if (is_string($os)) { $os = trim($os); if (strlen($os)) { $arr_sort_keys[] = md5($os); // save selected attribute and value for latter display $arr_display_attrs[$opt_name] = $FUNCS->excerpt($FUNCS->cleanXSS($os, 0, 'none'), 200); // adjust price $pp_price = $pp_price + $opt_values[0]['price']; } } else { $all_ok = 0; break; } } else { // select list if ($FUNCS->is_natural($os) && $os < count($opt_values)) { $arr_sort_keys[] = $os; // save selected attribute and value for latter display $arr_display_attrs[$opt_name] = $opt_values[$os]['attr']; // e.g Color=>Black; // adjust price $pp_price = $pp_price + $opt_values[$os]['price']; } else { $all_ok = 0; break; } } } } } // if all ok, add to cart if ($all_ok) { // create the sorting key - page_name + attributes $sorting_key = $FUNCS->make_key($arr_sort_keys); // create a unique id for this item. Will be passed on for future actions on cart. $unique_key = md5($sorting_key); // if item already exists in cart, update the original else add a new item. if (isset($this->items[$sorting_key])) { // update quantity $this->updated_rows[$sorting_key] = $this->items[$sorting_key]['quantity']; // save original quantity $this->items[$sorting_key]['quantity'] += $quantity; } else { $this->items[$sorting_key] = array('line_id' => $unique_key, 'id' => $pg->id, 'name' => $pg->page_name, 'title' => $pg->page_title, 'link' => K_SITE_URL . $pg->get_page_view_link(), 'price' => $pp_price, 'quantity' => $quantity, 'line_total' => 0, 'skip_line_total' => 0, 'options' => $arr_display_attrs, 'requires_shipping' => $pp_requires_shipping ? 1 : 0); // Add custom attributes if any foreach ($arr_custom_fields as $k => $v) { $this->items[$sorting_key][$k] = $v; } $this->updated_rows[$sorting_key] = 0; // sort ksort($this->items); } // finally persist in session $this->current_action_success = 1; $this->serialize(); } else { // report error? } } } } } $this->redirect(1); }