Skip to content

Commit

Permalink
Merge 8d0f9d4 into c634e63
Browse files Browse the repository at this point in the history
  • Loading branch information
gautampanday committed May 11, 2015
2 parents c634e63 + 8d0f9d4 commit 50bf841
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 52 deletions.
136 changes: 89 additions & 47 deletions templates/webshop/base.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
</div>
</footer>
<script type="text/html" id='cart-html-append'>

<a href="{{ url_for('nereid.cart.view_cart') }}" class="my-cart-show">{{ _('My Cart') }} <span class="quantity"><%= parseInt(user_status.cart_size) %></span></a>
<!-- Drop down cart -->
<div class="cart-item">
Expand Down Expand Up @@ -459,66 +460,79 @@
{% endblock flash_message_script %}

<script type="text/javascript">
var userDataCallbacks = $.Callbacks("");
function removeFromCart(url){
$.ajax({
type: "DELETE",
url: url,
})
.success(function(){
location.reload();
function disableBuyNow (lines) {
// Disable buy now button for already bought items from cart lines
// TODO: Handle case same way when product is removed from cart.
$.each(lines, function(index, line) {
$(".product-" + line.product.id + "-buy-now-btn")
.attr('disabled', 'disabled');
});
}
$.post("{{ url_for('nereid.website.user_status') }}", function(data){
var template = $("#cart-html-append").html();
$("li.my-cart").html(_.template(template, {'user_status': data.status}));
userDataCallbacks.fire(data);
$('.remove_product, .remove_from_cart').on('click', function(e){
var delUrl = $(this).data("url");
if(typeof ga == 'undefined'){
removeFromCart(delUrl);
return true;
}
function updatedCart(){
$.post("{{ url_for('nereid.website.user_status') }}", function(data){
var template = $("#cart-html-append").html();
$("li.my-cart").html(_.template(template, {'user_status': data.status}));
userDataCallbacks.fire(data);
var cart_items=data.status.cart.lines;
disableBuyNow(cart_items);
$('.remove_product, .remove_from_cart').on('click', function(e){
var delUrl = $(this).data("url");
if(typeof ga == 'undefined'){
removeFromCart(delUrl);
return true;
}
e.preventDefault();
e.preventDefault();
var alreadySubmitted = false;
var form = this;
var alreadySubmitted = false;
var form = this;
/* The code below registers a timeout which acts
* as a safety net. The boolean alreadySubmitted is used
* to check whether the form was already submitted or not.
* It is used by the callbacks in setTimeout and GA. It is to
* avoid the specific cases where both callbacks fire, resulting
* in a double form submission.
*/
/* The code below registers a timeout which acts
* as a safety net. The boolean alreadySubmitted is used
* to check whether the form was already submitted or not.
* It is used by the callbacks in setTimeout and GA. It is to
* avoid the specific cases where both callbacks fire, resulting
* in a double form submission.
*/
var t = setTimeout(function() {
if (alreadySubmitted) return;
alreadySubmitted = true;
removeFromCart(delUrl);
}, 500);
ga('ec:addProduct', {
'id': $(this).data('ga-product-id'),
'name': $(this).data('ga-product-name'),
'category': $(this).data('ga-product-category'),
'price': $(this).data('ga-product-price'),
'quantity': $(this).data('ga-product-quantity'),
});
ga('ec:setAction', 'remove');
ga('send', 'event', 'CartAnalytics', 'click', $(this).data('ga-event-label') || '', {
'hitCallback': function() {
var t = setTimeout(function() {
if (alreadySubmitted) return;
alreadySubmitted = true;
// Remove anyway
removeFromCart(delUrl);
}});
}, 500);
ga('ec:addProduct', {
'id': $(this).data('ga-product-id'),
'name': $(this).data('ga-product-name'),
'category': $(this).data('ga-product-category'),
'price': $(this).data('ga-product-price'),
'quantity': $(this).data('ga-product-quantity'),
});
ga('ec:setAction', 'remove');
ga('send', 'event', 'CartAnalytics', 'click', $(this).data('ga-event-label') || '', {
'hitCallback': function() {
if (alreadySubmitted) return;
alreadySubmitted = true;
// Remove anyway
removeFromCart(delUrl);
}});
});
});
});
};
var userDataCallbacks = $.Callbacks("");
function removeFromCart(url){
$.ajax({
type: "DELETE",
url: url,
})
.success(function(){
location.reload();
});
}
</script>
<script type="text/javascript">
$(function () {
updatedCart();
if (!sessionStorage.hide_site_message)
{
$("#site-message").removeClass("hidden");
Expand All @@ -529,6 +543,34 @@
})
});
</script>
<script type="text/javascript">
$('.product-buy-now').on('click', function(e){
var qty_element = $(this).siblings("input[name='quantity']");
var product_element = $(this).siblings("input[name='product']");
var new_quantity = parseInt(qty_element.val());
$.ajax({
type: "POST",
url: "{{ url_for('nereid.cart.add_to_cart') }}",
data: {'quantity': new_quantity, 'product': product_element.val()}
})
.success(function (result){
if (result.message == "OK"){
updatedCart();
}
})
.done(function(){
$.pnotify({
title: 'message',
text: 'Product Added to your Cart',
history: false,
nonblock: true,
delay:2000,
nonblock_opacity: .4,
animation: 'show'
});
});
});
</script>
<script>
$(document).ready(function(){
function resize_products () {
Expand Down
9 changes: 4 additions & 5 deletions templates/webshop/product.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@
<input type="hidden" name="size" id="size-id"/>
<input type="hidden" name="product" value="{{ product.id }}"/>
<input type="hidden" name="action" value="add"/>
<button class="btn btn-primary btn-buynow btn-block btn-lg" type="submit" {% if not product.can_buy_from_eshop() %}disabled{% endif %}
quantity="1" id="buy-now-btn">Buy Now</button>
<button class="btn btn-primary btn-buynow btn-block btn-lg product-buy-now product-{{ product.id }}-buy-now-btn" type="button" {% if not product.can_buy_from_eshop() %}disabled{% endif %}
quantity="1" id="buy-now-btn">Add to Cart</button>

</form>
</div>
{% if product.type == 'goods' %}
Expand Down Expand Up @@ -262,7 +263,7 @@
<input type="hidden" name="size" id="size-id"/>
<input type="hidden" name="product" value="{{ related_product.id }}"/>
<input type="hidden" name="action" value="add"/>
<button class="btn btn-related-item-add-cart" type="submit" quantity="1" id="buy-now-btn">Add to Cart</button>
<button class="btn btn-related-item-add-cart product-buy-now product-{{ related_product.id }}-buy-now-btn" type="button" quantity="1" id="buy-now-btn">Add to Cart</button>
</form>
{% endif %}
{% else %}
Expand Down Expand Up @@ -479,10 +480,8 @@
});
});
</script>

{% endblock scripts %}


{% block ga_page_view %}
// 1. Send product and impression data with pageview.
ga('ec:addProduct', {{ product.ga_product_data()|tojson|safe }});
Expand Down

0 comments on commit 50bf841

Please sign in to comment.