protected function save_order() { if (!$this->check_logged()) { \Messages::error('You must be logged in if you want to continue with your order.'); \Response::redirect(\Uri::create('order/checkout/address')); } // Save order $user = false; $order = false; $items = \Cart::items(); if (\Sentry::check()) { $user = \Sentry::user(); } if (\Input::post() && $items && $user) { $group_id = $user['groups'][0]['id']; $item_with_discount = array(); foreach ($items as $item) { $id = $item->get('id'); $product_groups = \Product\Model_Product_To_Groups::find_by_product_id($item->get('id')); foreach ($product_groups as $group) { $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $group_id, 'product_group_id' => $group->group_id), null, null, null); foreach ($all_discounts as $discount) { $discount = (int) $item_with_discount[$id]['discount'] + $discount->discount; $sub_total = $item->totalPrice(true) - (int) $discount / $item->totalPrice(true) * 100; $item_with_discount[$id] = array('product_group_id' => $group->product_id, 'user_group_id' => $group->group_id, 'discount' => $discount, 'sub_total' => $sub_total); } } $item_with_discount['total_discount'] = (int) $item_with_discount['total_discount'] + (int) $item_with_discount[$id]['total_discount']; $item_with_discount['total_price'] = (double) $item_with_discount['total_price'] + (double) $item_with_discount[$id]['sub_total']; } // check for a valid CSRF token if (!\Security::check_token()) { \Messages::error('CSRF attack or expired CSRF token.'); \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost'))); } try { // Update or create order if (is_numeric(\Session::get('order.id'))) { $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id')); } if (!$order) { $order = \Order\Model_Order::forge(); } $shipping_price = $order->shipping_price(null, null, true); $metadata = $user['metadata']; if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) { foreach ($billing as $key => $value) { $order->{$key} = $metadata[$key]; unset($metadata[$key]); } } foreach ($metadata as $key => $value) { $order->{$key} = $value; } $order->email = $user->get('email'); $order->user_id = $user->get('id'); $order->status = 'Pending'; $order->discount_amount = $item_with_discount['total_discount']; //\Cart::getTotal('price'); $order->total_price = $item_with_discount['total_price']; //\Cart::getTotal('price'); $order->finished = 1; $order->guest = $metadata['guest'] ? 1 : 0; $order->accepted = $metadata['master'] == 1 ? 1 : 0; $order->credit_account = $metadata['credit_account'] == 1 ? 1 : 0; $order->shipping_price = $shipping_price; // Save order, add products to order products if ($order->save()) { foreach ($items as $item) { $product_data = null; if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) { $product_data = \Product\Model_Product::product_data($product, $item->get('attributes')); } if ($product_data) { $order_products = \Order\Model_Products::forge(); $order_products->order_id = $order->id; $order_products->title = $product->title; $order_products->code = $product_data['code']; $order_products->price = $item->singlePrice(true); $order_products->price_type = $product_data['price_type']; $order_products->quantity = $item->get('quantity'); $order_products->product_id = $product->id; $order_products->artwork_required = $product->artwork_required; $order_products->artwork_free_over = $product->artwork_free_over; $order_products->subtotal = $item_with_discount[$item->get('id')]['sub_total']; //$item->totalPrice(true); $order_products->attributes = json_encode(\Product\Model_Attribute::get_combination($item->get('attributes'))); if (!empty($product->categories)) { $categories = array(); foreach ($product->categories as $category) { $categories[] = $category->title; } if ($categories) { $order_products->product_category = implode(',', $categories); } } $order_products->save(); // Find artworks if ($unique_id = $item->get('unique_id')) { if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) { $ysi = \Yousendit\Base::forge(); // Artworks (update, delete) foreach ($artworks as $artwork) { // Remove deleted artwork if ($artwork->deleted_at > 0) { $ysi->delete_artwork($artwork->file_id); $artwork->delete(); } else { $artwork->order_product_id = $order_products->id; $artwork->save(); } } } } } } } if ($order) { return $order; } else { return false; } } catch (\Database_Exception $e) { // show validation errors \Messages::error('There was an error while trying to save your order.'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); \Response::redirect(\Uri::create('order/checkout/cost')); } return false; } }
/** * 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 product categories $item->get_categories = static::lazy_load(function () use($item) { $categories = Model_Product_To_Categories::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'category_id'); if (!empty($categories)) { $categories = '(' . implode(',', array_keys($categories)) . ')'; return Model_Category::find(function ($query) use($categories) { return $query->where('id', 'IN', \DB::expr($categories)); }, 'id'); } return array(); }, $item->id, 'categories'); // Get product infotabs $item->get_infotabs = static::lazy_load(function () use($item) { return Model_Product_To_Infotabs::find(function ($query) use($item) { $query->order_by('sort', 'asc'); return $query->where('product_id', $item->id); }); }, $item->id, 'infotabs'); // Get product groups $item->get_groups = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; return Model_Group::find(function ($query) use($groups, $item) { $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'groups'); // Get product groups $item->get_pricing_group = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; $groups = Model_Group::find(function ($query) use($groups, $item) { $query->where('type', 'pricing'); $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }); return !empty($groups) ? $groups[0] : false; } return false; }, $item->id, 'pricing_group', 'object'); // Get related products $item->get_related_products = static::lazy_load(function () use($item) { $related = Model_Product_To_Related::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'related_id'); if (!empty($related)) { $related = '(' . implode(',', array_keys($related)) . ')'; return Model_Product::find(function ($query) use($related, $item) { $query->where('id', 'IN', \DB::expr($related)); $query->where('id', '<>', $item->id); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'related_products'); // Get upsell products $item->get_upsell_products = static::lazy_load(function () use($item) { $upsell = Model_Product_To_Upsell::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'upsell_id'); if (!empty($upsell)) { $upsell = '(' . implode(',', array_keys($upsell)) . ')'; return Model_Product::find(function ($query) use($upsell, $item) { $query->select(Model_Product_To_Upsell::get_protected('_table_name') . '.discount', Model_Product::get_protected('_table_name') . '.*'); $query->join(Model_Product_To_Upsell::get_protected('_table_name')); $query->on(Model_Product_To_Upsell::get_protected('_table_name') . '.upsell_id', '=', Model_Product::get_protected('_table_name') . '.id'); $query->where(Model_Product::get_protected('_table_name') . '.id', 'IN', \DB::expr($upsell)); $query->where(Model_Product::get_protected('_table_name') . '.id', '<>', $item->id); $query->order_by(Model_Product::get_protected('_table_name') . '.sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'upsell_products'); // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Product_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content files $item->get_files = static::lazy_load(function () use($item) { return Model_Product_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'files'); // Get content videos $item->get_videos = static::lazy_load(function () use($item) { return Model_Product_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'videos'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Product_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); // Get attributes $item->get_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id))); }, $item->id, 'attributes'); // Get default attribute $item->get_default_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id, 'default' => 1, 'active' => 1))); }, $item->id, 'default_attributes'); // Get all active prices $item->get_all_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get prices for product $item->get_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get unit price $item->get_unit_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'unit_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get sale price $item->get_sale_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'sale_price'); // Get default price (RRP or Sale price) $item->get_default_price = static::lazy_load(function () use($item) { $out = array(0); $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->and_where('default', 1); $query->limit(1); return $query; }, 'id'); if ($product_attributes) { $obj = reset($product_attributes); if ($obj->price > 0) { $out[0] = $obj->price; $out[1] = 'sale_price'; } else { $out[0] = $obj->retail_price; $out[1] = 'retail_price'; } } return $out; }, $item->id, 'default_price'); // Get active attribute group $item->get_active_attribute_group = static::lazy_load(function () use($item) { if ($attribute = Model_Attribute::find_one_by_product_id($item->id)) { return $attribute->attribute_group; } return false; }, $item->id, 'active_attribute_group'); // Get product data $item->get_data = static::lazy_load(function () use($item) { return \Product\Model_Product::product_data($item); }, $item->id, 'data'); } } } // return the result return $result; }
protected function save_order() { if (!$this->check_logged()) { \Messages::error('You must be logged in if you want to continue with your order.'); \Response::redirect(\Uri::create('order/checkout/address')); } // Save order $user = false; $order = false; $items = \Cart::items(); if (\Sentry::check()) { $user = \Sentry::user(); } if (\Input::post() && $items && $user) { // check for a valid CSRF token if (!\Security::check_token()) { \Messages::error('CSRF attack or expired CSRF token.'); \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost'))); } try { // Update or create order if (is_numeric(\Session::get('order.id'))) { $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id')); } if (!$order) { $order = \Order\Model_Order::forge(); } $cart_total = 0; foreach ($items as $item) { $product_data = null; if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) { $product_data = \Product\Model_Product::product_data($product, $item->get('attributes')); } $total_price = $item->totalDiscountedPrice(true); if (isset($product_data["price"]) && $product_data["price"] != 0) { $total_price = $product_data["price"] * $item->get('quantity'); } $cart_total += $total_price; } $method = \Input::post('delivery') == 'pickup' ? false : true; $shipping_price = $order->shipping_price(null, null, true, $method); $metadata = $user['metadata']; $data = array('email' => $user->get('email'), 'user_id' => $user->get('id'), 'status' => 'Pending', 'total_price' => $cart_total, 'finished' => 1, 'guest' => $metadata['guest'] ? 1 : 0, 'accepted' => $metadata['master'] == 1 ? 1 : 0, 'credit_account' => $metadata['credit_account'] == 1 ? 1 : 0, 'shipping_method' => \Input::post('delivery'), 'shipping_price' => $shipping_price, 'gst_price' => ($cart_total + $shipping_price) * 0.1); // $order->discount_amount = $item_with_discount['total_discount'];//\Cart::getTotal('price'); //\Cart::getDiscountedTotal('price');//\Cart::getTotal('price'); if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) { foreach ($billing as $key => $value) { $data[$key] = $metadata[$key]; unset($metadata[$key]); } } foreach ($metadata as $key => $value) { $data[$key] = $value; } $order->set($data); // Save order, add products to order products if ($order->save()) { foreach ($items as $item) { $product_data = null; if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) { $product_data = \Product\Model_Product::product_data($product, $item->get('attributes')); } $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id))); if ($product_data && !$item_exists) { $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $item->get('quantity'), 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_data['price'] * $item->get('quantity'), 'attributes' => json_encode(\Product\Model_Attribute::get_combination($item->get('attributes'))), 'attributes_id' => $item->get('attributes'))); //$item->singleDiscountedPrice(true);//$item->singlePrice(true); //$item->totalDiscountedPrice(true);//$item->totalPrice(true); if (!empty($product->categories)) { $categories = array(); foreach ($product->categories as $category) { $categories[] = $category->title; } if ($categories) { $order_products->product_category = implode(',', $categories); } } // update stock quantity \Product\Model_Attribute::update_stock_quantity($item->get('attributes'), $item->get('quantity')); $order_products->save(); // Find artworks if ($unique_id = $item->get('unique_id')) { if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) { $ysi = \Yousendit\Base::forge(); // Artworks (update, delete) foreach ($artworks as $artwork) { // Remove deleted artwork if ($artwork->deleted_at > 0) { $ysi->delete_artwork($artwork->file_id); $artwork->delete(); } else { $artwork->set(array('order_product_id' => $order_products->id)); $artwork->save(); } } } } } } } if ($order) { return $order; } else { return false; } } catch (\Database_Exception $e) { // show validation errors \Messages::error('There was an error while trying to save your order.'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); \Response::redirect(\Uri::create('order/checkout/cost')); } return false; } }
<tr> <td width="50%">Product Name</td> <td width="35%">Quantity</td> <td width="15%">Price</td> </tr> <!-- Display Items in Cart --> <?php if (!empty($items)) { ?> <?php $cart_total = 0; foreach ($items as $item) { $product_data = null; if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) { $product_data = \Product\Model_Product::product_data($product, $item->get('attributes')); extract($product_data); } $price = $item->singlePrice(true); $total_price = $item->totalDiscountedPrice(true); if (isset($product_data["price"]) && $product_data["price"] != 0) { $price = $product_data["price"]; $total_price = number_format($product_data["price"], 2) * $item->get('quantity'); } $cart_total += $total_price; ?> <tr> <td><?php echo $item->get('title'); ?> </td>
protected function save_order($user) { $order = false; try { // Update or create order if (is_numeric(\Session::get('admin_order.id'))) { $order = \Order\Model_Order::find_one_by_id(\Session::get('admin_order.id')); } if (!$order) { $order = \Order\Model_Order::forge(); } $data = array('user_id' => $user->id, 'status' => \Input::post('status'), 'title' => $user->get('metadata.title'), 'first_name' => $user->get('metadata.first_name'), 'last_name' => $user->get('metadata.last_name'), 'company' => $user->get('metadata.company'), 'address' => $user->get('metadata.address'), 'address2' => null, 'suburb' => $user->get('metadata.suburb'), 'postcode' => $user->get('metadata.postcode'), 'country' => $user->get('metadata.country'), 'state' => $user->get('metadata.state'), 'phone' => $user->get('metadata.phone'), 'email' => $user->get('email'), 'guest' => 1, 'total_price' => \Input::post('products_total_hidden'), 'discount_amount' => \Input::post('discount_hidden'), 'shipping_method' => null, 'shipping_price' => \Input::post('shipping_hidden'), 'gst_price' => \Input::post('gst_amount_hidden'), 'finished' => 1, 'accepted' => 0, 'credit_account' => 0); $order->set($data); $product_id = \Input::post('product_id'); $product_quantity = \Input::post('product_quantity'); $product_price = \Input::post('product_price'); // Save order, add products to order products if ($order->save()) { foreach ($product_id as $key => $item) { $product_data = null; if ($product = \Product\Model_Product::find_one_by_id($item)) { $product_data = \Product\Model_Product::product_data($product, null); } $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id))); if ($product_data && !$item_exists) { $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $product_quantity[$key], 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_price[$key] * $product_quantity[$key], 'attributes' => json_encode(\Product\Model_Attribute::get_combination(0)))); if (!empty($product->categories)) { $categories = array(); foreach ($product->categories as $category) { $categories[] = $category->title; } if ($categories) { $order_products->product_category = implode(',', $categories); } } // update stock quantity \Product\Model_Attribute::update_stock_quantity(0, $product_quantity[$key]); $order_products->save(); } } } if ($order) { return $order; } else { return false; } } catch (\Database_Exception $e) { // show validation errors \Messages::error('There was an error while trying to save your order.'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); \Response::redirect(\Uri::create('admin/order/create')); } return false; }
<?php if ($key > 0 && $key % 5 == 0) { echo '<div class="clear"></div>'; } ?> <div class="images"> <div class="col-sm-4"> <div class="product-item"> <a href="<?php echo \Uri::create('product/' . $item->seo->slug); ?> "> <div class="product-img"> <?php // Get product data $product_data = \Product\Model_Product::product_data($item); ?> <?php if (!empty($product_data['images']) && is_file($images_base_path . 'medium/' . $product_data['images'][0]->image)) { ?> <?php echo \Html::img(\Uri::create($images_base_url . 'medium/' . $product_data['images'][0]->image), array('width' => 'auto', 'height' => 321, 'alt' => $product_data['images'][0]->alt_text)); ?> <?php } else { ?> <div class="no-image"> <i class="icon icon-image2"></i> <h4>No image</h4> </div> <?php