public function addCart($data) { // get data post $product_id = $data['product_id']; $colors = $data['colors']; $print = $data['print']; $quantity = $data['quantity']; // get attribute if (isset($data['attribute'])) { $attribute = $data['attribute']; } else { $attribute = false; } if ($quantity < 1) { $quantity = 1; } $time = strtotime("now"); if (isset($data['cliparts'])) { $cliparts = $data['cliparts']; } else { $cliparts = false; } $content = array(); $content['error'] = 1; // load product $products = $this->getProducts(); $product = false; for ($i = 0; $i < count($products); $i++) { if ($product_id == $products[$i]->id) { $product = $products[$i]; break; } } if ($product === false) { $content['msg'] = 'Product could not be found'; } else { $content['error'] = 0; // load cart include_once ROOT . DS . 'includes' . DS . 'cart.php'; $cart = new dgCart(); $post = array('colors' => $colors, 'print' => $print, 'attribute' => $attribute, 'quantity' => $quantity, 'product_id' => $product_id); // load setting $setting = $this->getSetting(); include_once ROOT . DS . 'includes' . DS . 'addons.php'; $addons = new addons(); $params = array('data' => $data, 'product' => $product, 'setting' => $setting); $addons->view('hooks' . DS . 'product', $params); $result = $cart->totalPrice($product, $post, $setting); $params = array('data' => $data, 'product' => $product, 'setting' => $setting, 'result' => $result); $addons->view('hooks' . DS . 'fields', $params); $result->product = new stdClass(); $result->product->name = $product->title; $result->product->sku = $product->sku; // get cliparts $clipartsPrice = array(); if (isset($data['cliparts']) && count($data['cliparts']) > 0) { $clipartsPrice = $cart->getPriceArt($data['cliparts']); } $result->cliparts = $clipartsPrice; $total = new stdClass(); $total->old = $result->price->colors + $result->price->prints; $print_discount = 0; // 2015.11.21 if (isset($result->price->print_discount)) { $print_discount = $result->price->print_discount; } // 2015.11.21 $total->sale = $result->price->colors + $result->price->prints - $print_discount; // 2015.11.21 if (count($result->cliparts)) { foreach ($result->cliparts as $id => $amount) { $total->old = $total->old + $amount; $total->sale = $total->sale + $amount; } } if (empty($result->price->attribute)) { $result->price->attribute = 0; } $total->old = ($total->old + $result->price->attribute) * $quantity; $total->sale = ($total->sale + $result->price->attribute) * $quantity; $result->total = $total; // get symbol if (!isset($setting->currency_symbol)) { $setting->currency_symbol = '$'; } $result->symbol = $setting->currency_symbol; // save file image design $path = $this->folder(); $design = array(); $design['images'] = array(); if (isset($data['design']['images']['front'])) { $design['images']['front'] = $this->createFile($data['design']['images']['front'], $path, 'cart-front-' . $time); } if (isset($data['design']['images']['back'])) { $design['images']['back'] = $this->createFile($data['design']['images']['back'], $path, 'cart-back-' . $time); } if (isset($data['design']['images']['left'])) { $design['images']['left'] = $this->createFile($data['design']['images']['left'], $path, 'cart-left-' . $time); } if (isset($data['design']['images']['right'])) { $design['images']['right'] = $this->createFile($data['design']['images']['right'], $path, 'cart-right-' . $time); } if (empty($result->options)) { $result->options = array(); } if (isset($data['teams'])) { $teams = $data['teams']; } else { $teams = ''; } $params = array('data' => $data, 'result' => $result, 'design' => $design, 'setting' => $setting); $addons->view('hooks' . DS . 'cart', $params); // add cart $item = array('id' => $result->product->sku, 'product_id' => $data['product_id'], 'qty' => $data['quantity'], 'teams' => $teams, 'price' => $result->total->sale, 'prices' => json_encode($result->price), 'cliparts' => json_encode($result->cliparts), 'symbol' => $result->symbol, 'customPrice' => $result->price->attribute, 'name' => $result->product->name, 'time' => $time, 'options' => $result->options); $rowid = md5($result->product->sku . $time); $cache = $this->cache('cart'); $designs = array('color' => $data['colors'][key($data['colors'])], 'images' => $design['images'], 'vector' => $data['design']['vectors'], 'fonts' => $data['fonts'], 'item' => $item); $cache->set($rowid, $designs); $price_product = $result->total->sale / $quantity; $content['product'] = array('rowid' => $rowid, 'price' => $price_product, 'quantity' => $quantity, 'color_hex' => $data['colors'][key($data['colors'])], 'color_title' => $product->design->color_title[key($data['colors'])], 'images' => json_encode($design['images']), 'teams' => $teams, 'options' => $result->options); } return $content; }