blob: eaa1fab5535a91d12764238492ffd713c745675f [file] [log] [blame]
Anand Doshiab690292013-06-13 11:21:35 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17// js inside blog page
18
19$(document).ready(function() {
20 // make list of items in the cart
21 wn.cart.render();
Anand Doshiabc10032013-06-14 17:44:03 +053022 wn.cart.bind_events();
Anand Doshiab690292013-06-13 11:21:35 +053023});
24
25// shopping cart
26if(!wn.cart) wn.cart = {};
27$.extend(wn.cart, {
28 render: function() {
29 var $cart_wrapper = $("#cart-added-items").empty();
30 if(Object.keys(wn.cart.get_cart()).length) {
31 $('<div class="row">\
Anand Doshiabc10032013-06-14 17:44:03 +053032 <div class="col col-lg-9 col-sm-9">\
Anand Doshiab690292013-06-13 11:21:35 +053033 <div class="row">\
34 <div class="col col-lg-3"></div>\
35 <div class="col col-lg-9"><strong>Item Details</strong></div>\
36 </div>\
37 </div>\
Anand Doshiabc10032013-06-14 17:44:03 +053038 <div class="col col-lg-3 col-sm-3"><strong>Qty</strong></div>\
Anand Doshiab690292013-06-13 11:21:35 +053039 </div><hr>').appendTo($cart_wrapper);
40
41 $.each(wn.cart.get_cart(), function(item_code, item) {
42 item.image_html = item.image ?
43 '<div style="height: 120px; overflow: hidden;"><img src="' + item.image + '" /></div>' :
44 '{% include "app/website/templates/html/product_missing_image.html" %}';
Anand Doshiabc10032013-06-14 17:44:03 +053045 item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : "";
Anand Doshiab690292013-06-13 11:21:35 +053046
47 $(repl('<div class="row">\
Anand Doshiabc10032013-06-14 17:44:03 +053048 <div class="col col-lg-9 col-sm-9">\
Anand Doshiab690292013-06-13 11:21:35 +053049 <div class="row">\
50 <div class="col col-lg-3">%(image_html)s</div>\
51 <div class="col col-lg-9">\
52 <h4><a href="%(url)s">%(item_name)s</a></h4>\
53 <p>%(description)s</p>\
54 </div>\
55 </div>\
56 </div>\
Anand Doshiabc10032013-06-14 17:44:03 +053057 <div class="col col-lg-3 col-sm-3">\
58 <p><input type="text" placeholder="Qty" value="%(qty)s" \
59 item_code="%(item_code)s" class="cart-input-qty"></p>\
Anand Doshiab690292013-06-13 11:21:35 +053060 %(price_html)s\
61 </div>\
62 </div><hr>', item)).appendTo($cart_wrapper);
63 });
Anand Doshiabc10032013-06-14 17:44:03 +053064
65 $('<p class="text-right"><button type="button" class="btn btn-success checkout-btn">\
66 <span class="icon-ok"></span> Checkout</button></p>')
67 .appendTo($cart_wrapper);
68
Anand Doshiab690292013-06-13 11:21:35 +053069 } else {
70 $('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper);
71 }
Anand Doshiabc10032013-06-14 17:44:03 +053072 },
73
74 bind_events: function() {
75 // on change of qty
76 $(".cart-input-qty").on("change", function on_change_of_qty() {
77 wn.cart.set_value_in_cart($(this).attr("item_code"), "qty", $(this).val());
78 });
79
80 // shopping cart button
81 $(".checkout-btn").on("click", function() {
82 console.log("checkout!");
83 console.log(wn.cart.get_cart());
84
85 var user_is_logged_in = getCookie("full_name");
86 if(user_is_logged_in) {
87 wn.call({
88 method: "website.helpers.cart.checkout",
89 args: {cart: wn.cart.get_cart()},
90 btn: this,
91 callback: function(r) {
92 console.log(r);
93 }
94 });
95 } else {
96 window.location.href = "login?from=cart";
97 }
98 });
Anand Doshiab690292013-06-13 11:21:35 +053099 }
100});