function testShouldRemoveWhenSetQuantity0() { $cart = new Cart(); $cart->add(5); $cart->setQuantity(5, 0); $this->assertEquals(array(), $cart->items(), 'should remove when set quantity to 0'); }
public function action_cart_listing($type = 'cart') { if (!\Input::is_ajax()) { die('Access Is Not Allowed!'); } $out = array(); $out['cart_empty'] = 0; $items = \Cart::items(); // Cart is empty? if (!$items) { $out['cart_empty'] = 1; } // Product table $out['product_table'] = \Theme::instance()->view('views/order/_partials/product_listing_table', array('items' => $items), false)->render(); // Price table $out['price_table'] = \Theme::instance()->view('views/order/_partials/product_price_table', array('type' => $type), false)->render(); echo json_encode($out); exit; }
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; } }
public function action_get_discount() { $success = false; $valid = false; $lbl = 'apply'; $f_discount = 0.0; $grand_total = 0.0; $gst_price = \Input::post('gst_price'); // remove discount if (\Input::post('action') == 'remove') { $method = \Input::post('method') == 'pickup' ? false : true; $order = \Order\Model_Order::forge(); $items = \Cart::items(); $cart_total = 0; foreach ($items as $item) { $total_price = $item->totalDiscountedPrice(true); $cart_total += $total_price; } $total = \Helper::total_price(array($cart_total, $return['shipping_price'])); $shipping_price = $order->shipping_price(null, null, true, $method); $gst_price = number_format(($cart_total + $shipping_price) * 0.1, 2, '.', ''); $grand_total = number_format($cart_total + $shipping_price + ($cart_total + $shipping_price) * 0.1, 2, '.', ''); $success = true; if (is_numeric(\Session::get('order.id'))) { $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id')); $order->set(array('id_discount' => NULL, 'discount_amount' => 0, 'total_price' => $grand_total, 'gst_price' => $gst_price)); $order->save(); } } else { $discount = \Discountcode\Model_Discountcode::find_one_by_code(\Input::post('discount_code')); if ($discount && $discount->status == 'active') { // check for expiry date if ($discount->active_from != '0000-00-00' && $discount->active_to != '0000-00-00') { $current_date = strtotime(date('Y-m-d')); $from = strtotime($discount->active_from); $to = strtotime($discount->active_to); if ($current_date >= $from && $current_date <= $to) { $valid = true; } } else { $valid = true; } if ($valid) { $valid = false; // check for single/multiple use if ($discount->use_type == 'multi use') { $valid = true; } else { // check if discount code was already used. $order_exists = \Order\Model_Order::find_one_by_id_discount($discount->id); if (is_null($order_exists)) { $valid = true; } } } if ($valid) { 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(); } $method = \Input::post('method') == 'pickup' ? false : true; $shipping_price = $order->shipping_price(null, null, true, $method); // compute value of discount switch ($discount->type) { case 'free shipping': $f_discount = \Input::post('method') == 'pickup' ? 0 : $shipping_price; break; case 'percentage': $f_discount = \Input::post('total_price') * ($discount->type_value / 100); break; case 'value': $f_discount = $discount->type_value; break; } $gst_price = number_format((\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', ''); $grand_total = number_format(\Input::post('total_price') + $shipping_price + (\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', ''); $order->set(array('finished' => 0, 'id_discount' => $discount->id, 'discount_amount' => $f_discount, 'shipping_price' => $shipping_price, 'gst_price' => $gst_price, 'total_price' => $grand_total)); $order->save(); \Session::set('order.id', $order->id); $lbl = 'remove'; $success = true; } } } echo json_encode(array('success' => $success, 'btn_label' => $lbl, 'discount_amount' => $f_discount, 'grand_total' => $grand_total, 'gst_price' => $gst_price)); }
?> </a> </li> </ul> </div> <div class="app-drawer"> <nav class="nav-menu nav-stroked"> <ul> <li> <a href="<?php echo \Uri::create('order/cart/edit_cart'); ?> "><i class="icon icon-cart"></i> <span class="js-counter-view total_cart_items"><?php echo count(\Cart::items()); ?> </span></a> </li> <li> <div class="search-toggle-btn"> <i class="icon icon-search"></i> </div> </li> </ul> <div class="search-toggle"> <div class="search-wrapper"> <div class="search-wrapper-inner"> <?php echo \Form::open(array('action' => \Uri::create('search'), 'class' => 'form-search', 'id' => 'form-search', 'method' => 'get')); ?>
public function action_cart_info() { $out = array(); $out['total'] = \Cart::total(); $out['item_count'] = \Cart::itemCount(); $out['items'] = count(\Cart::items()); echo json_encode($out); exit; }