I am trying to get the Cart2 module to perform an ajax request rather than submitting the page.
I am using javascript to display that an item is added to the basket. unfortunately the page refreshes and the javascript instance is lost.
any thoughts welcome
Thanks
Cart2 Ajax calls
-
- Support Guru
- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
- Location: Fernie British Columbia, Canada
Re: Cart2 Ajax calls
the add to cart action is designed to redirect to a destination page after submission. Ajax won't be possible there till the action is rewritten.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: Cart2 Ajax calls
@calguy1000
Is there a way of the Cart2 module exposing the sku's currently held in the cart so that on load I can tell the page to scroll the sku id I have assigned to the product widget? this may be a solution I could explore to solve my issue in the short term?
Thanks
Is there a way of the Cart2 module exposing the sku's currently held in the cart so that on load I can tell the page to scroll the sku id I have assigned to the product widget? this may be a solution I could explore to solve my issue in the short term?
Thanks
-
- Support Guru
- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
- Location: Fernie British Columbia, Canada
Re: Cart2 Ajax calls
The view cart template has the sku (if it exists) of every item in the cart.
You may be able to repurpose a call to the viewcart action with a different template to do what you want.
You may be able to repurpose a call to the viewcart action with a different template to do what you want.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: Cart2 Ajax calls
This code will help you add products and not refresh the page.
MyCart
Addtocart
Code: Select all
<head>
<__script__ type="text/javascript" src="assets/js/jquery.min.js"></__script>
</head>
MyCart
Code: Select all
<ul id="mycart" class="navbar-mini-cart navbar-nav animate-dropdown nav flip">
<li class="nav-item dropdown">
<a href="{cms_selflink href='korzina'}" class="nav-link">
<i class="ec ec-shopping-bag"></i>
{strip}
{if $cart_itemcount == 0}
{*Пустая корзина*}
{else if $cart_itemcount >= "100"}
<span class="cart-items-count count">∞ </span>
{else}
<span class="cart-items-count count">{$cart_itemcount}</span>
{/if}
{/strip}
<span class="cart-items-total-price total-price">
{strip}
{if $cart_itemcount == 0}
Корзина пуста
{else}
<span class="amount">
{$cart_totalprice} {$currency_symbol}
</span>
{/if}
{/strip}
</span>
</a>
</li>
</ul>
Addtocart
Code: Select all
<div id="{$submitname}_cart" class="frm_cart">
{$formstart}
{strip}
<input type="hidden" name="{$submitname}" value="1" style="display: none;" />
<div class="input-group">
<input class="form-control " type="text" value="1" name="{$quantityname}" placeholder="1" size="3" maxlength="3"/>
<span class="input-group-btn">
<button type="submit" name="{$submitname}" class="btn btn-danger btn-lg" ><i class="ec ec-add-to-cart"></i> Купить </button>
</span>
</div>
{/strip}
{$formend}
</div>
{jsmin}
{literal}
<__script__ type="text/javascript">
function ajaxifyCartButtons_{/literal}{$submitname}{literal}() {
$('#{/literal}{$submitname}{literal}_cart form').on( "submit", function( event ) {
event.preventDefault();
var posturl = $(this).attr("action") + "?showtemplate=false";
var postdata = $(this).serialize();
/* ajax */
$.post(posturl,postdata,function() {
//we don't display returned data
// but update cart
updateCart_{/literal}{$submitname}{literal}();
replaceButton_{/literal}{$submitname}{literal}();
});
});
}
function updateCart_{/literal}{$submitname}{literal}() {
$("#mycart").load(
"{/literal}{module_action_link module='Cart2' action='mycart' urlonly=1 jsfriendly=1}{literal}",
{ 'showtemplate' : 'false' },
function() {}
)
};
function replaceButton_{/literal}{$submitname}{literal}() {
$("button[name~='{/literal}{$submitname}{literal}']").replaceWith('<button type="submit" class="btn btn-warning ripple" disabled> <i class="fa fa-check" aria-hidden="true"></i> Добавлено <span class="md-ripple"></span></button>');
};
ajaxifyCartButtons_{/literal}{$submitname}{literal}();
</__script>
{/literal}
{/jsmin}