Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

AJAX based Shopping Cart
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

Hi,

Was wondering if I downloaded the AjaxPro cart plugin can I edit the code to customise it, or does the whole Magento connect thing stop me from doing this (don’t really see how it can, just want to check before I spend money on it)

 
Magento Community Magento Community
Magento Community
Magento Community
 
magextend
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2009-09-15
France Lyon
 
nar - 03 October 2009 08:30 PM

magextend - 15 September 2009 10:09 PM
Hi,

Our team just released the module developed by chrissou

It is available here : http://www.magextend.com/en/easyajaxproduct.php

Regards,

MageXtend

I tried the demo in mozilla, ie, and chrome.  When I press “add to cart” all take me to the “enable cookies” page.  But my cookies are e&nabled;.  If I press “back” then it lets me add to cart with without ajax.

Magextend just released a new version of Easy Ajax Product (v 1.1) correcting all the bugs (a lot in IE) you can now view the new version on our website : Easy Ajax Product

nar - 03 October 2009 08:40 PM

aheadWorks - 04 May 2009 03:41 AM
srinigenie - 12 March 2009 10:10 PM
Check this one out. Looks interesting

http://www.magentocommerce.com/extension/969/ajaxcart-premium

@ twistedindustries - Would have loved to help you on this one. But felt you might have got a better hand to help. Sorry to hear that you did not.

There is a cheaper alternative by the way:
http://www.magentocommerce.com/extension/1185/ajax-cart-pro

I would love to buy this extension, but without ajax enabled “add to wishlist”, “add to compare”, “update cart”, etc., it just seems unfinished.  I also suspect that I am not the only one that feels this way.  Please let me know if these features can be added.

Thanks!

The new version 1.1 of Easy Ajax Product is really optimized for all navigators (Firefox, Internet Explorer, Chrome, Opera, Safari, ...) and on general for all the common web renderer.

The admin panel have also a lot of configuration options (disable ajax wiishlist and/or compare for example)

Feel free to send us an email for any question ( )

Regards,

Magextend

 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

Ok I’ve had a play around with this myself. I changed the CartController to sen back only the sidecart if a product is added using an ajax request.

However I seem to be having problems in Firefox, the whole page is being returned even when the cart controllers being called using ajax. I’m using $this->getRequest()->isXmlHttpRequest() to detect if it’s an ajax request. Any ideas?

<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';
class 
Moesian_AjaxCart_Checkout_CartController extends Mage_Checkout_CartController
{
    
# Overloaded indexAction
    /**
     * Shopping cart display action
     */
    
public function indexAction()
    
{
        $cart 
$this->_getCart();
        if (
$cart->getQuote()->getItemsCount()) {
            $cart
->init();
            
$cart->save();

            if (!
$this->_getQuote()->validateMinimumAmount()) {
                $warning 
Mage::getStoreConfig('sales/minimum_order/description');
                
$cart->getCheckoutSession()->addNotice($warning);
            
}
        }

        
foreach ($cart->getQuote()->getMessages() as $message{
            
if ($message{
                $cart
->getCheckoutSession()->addMessage($message);
            
}
        }

        
/**
         * if customer enteres shopping cart we should mark quote
         * as modified bc he can has checkout page in another window.
         
        $this->_getSession()->setCartWasUpdated(true);
        
            $this->loadLayout();    
            $this->getResponse()->setBody($this->_getSideCartHtml());
            */
        /**
         * if we have an ajax request return the cart_sidebar
         * otherwise load the page as normal
         */        
        
if($this->getRequest()->isXmlHttpRequest())
        
{
            $this
->loadLayout();    
            
$this->getResponse()->setBody($this->_getSideCartHtml());
        
}
        
else
        
{
            Varien_Profiler
::start(__METHOD__ 'cart_display');
            
$this->loadLayout();
            
$this->_initLayoutMessages('checkout/session');
            
$this->_initLayoutMessages('catalog/session');
            
$this->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart Ajax 2'));
            
$this->renderLayout();
            
Varien_Profiler::stop(__METHOD__ 'cart_display');
        
}
    }
    
/**
     * Add product to shopping cart action
     */
    
public function addAction()
    
{
        $cart   
$this->_getCart();
        
$params $this->getRequest()->getParams();
        
$product$this->_initProduct();
        
$related$this->getRequest()->getParam('related_product');

        
/**
         * Check product availability
         */
        
if (!$product{
            $this
->_goBack();
            return;
        
}


        
try {
            $cart
->addProduct($product$params);
            if (!empty(
$related)) {
                $cart
->addProductsByIds(explode(','$related));
            
}

            $cart
->save();

            
$this->_getSession()->setCartWasUpdated(true);

            
/**
             * @todo remove wishlist observer processAddToCart
             */
            
Mage::dispatchEvent('checkout_cart_add_product_complete',
                array(
'product' => $product'request' => $this->getRequest(), 'response' => $this->getResponse())
            );
            
$message $this->__('%s was successfully added to your shopping cart.'$product->getName());
            if (!
$this->_getSession()->getNoCartRedirect(true)) {
                $this
->_getSession()->addSuccess($message);
                
$this->_goBack();
            
}
        }
        
catch (Mage_Core_Exception $e{
            
if ($this->_getSession()->getUseNotice(true)) {
                $this
->_getSession()->addNotice($e->getMessage());
            
else {
                $messages 
array_unique(explode("\n"$e->getMessage()));
                foreach (
$messages as $message{
                    $this
->_getSession()->addError($message);
                
}
            }

            $url 
$this->_getSession()->getRedirectUrl(true);
            if (
$url{
                $this
->getResponse()->setRedirect($url);
            
else {
                $this
->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
            
}
        }
        
catch (Exception $e{
            $this
->_getSession()->addException($e$this->__('Can not add item to shopping cart'));
            
$this->_goBack();
        
}
    }

    
protected function _getSideCartHtml()
    
{

        $output 
$this->getLayout()->getBlock('cart_sidebar')->toHtml();
        return 
$output;
    
}
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Imagetag
Jr. Member
 
Total Posts:  9
Joined:  2009-09-19
 

I had to change the goBack function as well. Be sure, that the redirection mechanism doesn’t start. I used an extra variable is_ajax and added this to my requests.

To check the request status an var values, I added them temporarily to the success message, debugging is quite annoying in Magento.

Hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShopGuy
Guru
 
Total Posts:  462
Joined:  2008-09-07
 

Mage Extend,

When you add an item to the cart a popup shows asking if they want to be redirected to their cart or continue shopping. I have a few questions for you:

1) Is the time the popup auto closes adjustable? (3 seconds, 5 seconds, etc, unlimited seconds)

2) Can the popup be configured in the admin to say something when adding to wishlist or compare (goto wishlist or continue browsing, etc)

 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

Many thanks Imagetag, you’ve talked me down from the roof wink

Here’s what I came up with seems to be working fine so far.

<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';

class 
Moesian_AjaxCart_Checkout_CartController extends Mage_Checkout_CartController
{
    
# Overloaded indexAction
    /**
     * Shopping cart display action
     */
    
public function indexAction()
    
{

        $cart 
$this->_getCart();
        if (
$cart->getQuote()->getItemsCount()) {
            $cart
->init();
            
$cart->save();

            if (!
$this->_getQuote()->validateMinimumAmount()) {
                $warning 
Mage::getStoreConfig('sales/minimum_order/description');
                
$cart->getCheckoutSession()->addNotice($warning);
            
}
        }

        
foreach ($cart->getQuote()->getMessages() as $message{
            
if ($message{
                $cart
->getCheckoutSession()->addMessage($message);
            
}
        }

        
/**
         * if customer enteres shopping cart we should mark quote
         * as modified bc he can has checkout page in another window.
         
        $this->_getSession()->setCartWasUpdated(true);
        
            $this->loadLayout();    
            $this->getResponse()->setBody($this->_getSideCartHtml());
            */
        /**
         * if we have an ajax request return the cart_sidebar
         * otherwise load the page as normal
         */        
        
if($this->getRequest()->isXmlHttpRequest())
        
{
            $this
->_sendSideCartHtml();
        
}
        
else
        
{
            Varien_Profiler
::start(__METHOD__ 'cart_display');
            
$this->loadLayout();
            
$this->_initLayoutMessages('checkout/session');
            
$this->_initLayoutMessages('catalog/session');
            
$this->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart Ajax 2'));
            
$this->renderLayout();
            
Varien_Profiler::stop(__METHOD__ 'cart_display');
        
}
    }


    
protected function _sendSideCartHtml()
    
{
        $this
->loadLayout();    
          
$output $this->getLayout()->getBlock('cart_sidebar')->toHtml();
        
$this->getResponse()->setBody($output);
    
}
    
    
/**
     * Set back redirect url to response
     *
     * @return Mage_Checkout_CartController
     */
    
protected function _goBack()
    
{
    
        
if($this->getRequest()->isXmlHttpRequest())
        
{
            $this
->_sendSideCartHtml();
            
        
}
        
else
        
{
            
if (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
                && !
$this->getRequest()->getParam('in_cart')
                && 
$backUrl $this->_getRefererUrl()) {
    
                $this
->getResponse()->setRedirect($backUrl);
            
else {
                
if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
                    $this
->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
                
}
                $this
->_redirect('checkout/cart');
            
}
            
return $this;
        
}
    }
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Imagetag
Jr. Member
 
Total Posts:  9
Joined:  2009-09-19
 

Great!

In case you’d like to know. I used the output messages and the cart content to build a json answer. The message is displayed in a usual div on the page, and the cart content updates, hum the cart wink .

Haven’t checked your code, but in my case I had to stop the cart controller from adding it’s messages to the output, because all these messages showed up, when I opened the checkout/cart.

If you’re going to transport the cart html via json, too, be sure to use

htmlentities($block->toHtml(),ENT_NOQUOTES,"UTF-8")

It was a pain in the ... you know where, before I found that…

 
Magento Community Magento Community
Magento Community
Magento Community
 
pamela55
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2009-03-09
 

Hi Moesian, thanks for the code.

But I’m still not sure where to add it. Can you tell me the path of the file?
Or any information to help me understand better?

Cheers,
Pam

 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

I used this tutorial to get the point where I could modify the cart controller http://www.magentocommerce.com/wiki/how_to_overload_a_controller. If you follow that and put in the code I’ve posted that should get you most of the way.

You’ll need to make some alterations to the products templates. I made the forms submit normally without javascript (not sure if this is a great idea), and added a css class to the form ‘.ajax-add-to-cart’. I then used some javascript to ajaxify forms with the classname ‘.ajax-add-to-cart’. Here’s the js code you’ll need.

var shop {

    init
: function() {

    
        shop
.cart.init();
    
},
    
    
cart{
        init
: function(){
            
var product_forms  = $$('.ajax-add-to-cart');
            if (!
product_forms[0]) return;
            
product_forms.invoke('observe''submit'shop.cart.add);
        
},
        
add: function(e){
            
var ele e.element();
            
            
ele.request({
                onFailure
: function() {},
                
onSuccess: function(t{$('header-cart').replace(t.responseText); }
                }
);
            
Event.stop(e); // stop the form from submitting

        
}
    }
}
try {
    document
.observe('dom:loaded'shop.init);

}
 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

Thanks for the tip on the json stuff ImageTag i’ll look into this. Does using json have any performance benefit? From the line of code you gave it it looks like your still rendering the cart html to get the values your sending back. I did think it would be worth using json if i could skip this part out just spit out the relevant values, not bothering to build the cart html (don’t know if this would speed up the request or not really).

And if it’s not performance then why else would json be a good idea (pretty much in the dark with this sort of thing).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Imagetag
Jr. Member
 
Total Posts:  9
Joined:  2009-09-19
 

Yes I return the card, but it would be absolutely no problem, to send back only an array of products, or the new total etc. You could then use the prototype update function to update all parameters in place, instead of the whole cart..

The main advantage for me was, that I’m not only returnig the cart, but the system messages, too. So I haven’t to write any custom strings for my module, needn’t care about translations and stuff. Of course, if you don’t need to transport more than the cart html, you need no json.

Maybe I make some speed testig afterall and see, if there’s a performance advantage, when the cart isn’t re-rendered, but for now I have to finish the templates an maybe extend this ajax stuff to work for the wishlist (and product comparisons maybe?? ), too.

 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

Hi,

The whole json thing seems pretty cool, should be using it more often. I’ve made a few changes to my cart but I’m having problems outputting the messages collect, I’m trying to grab them from the session object but the message array is coming out blank. Here’s the function I’m using to build and send the json object.

protected function _sendJsonCart()
    
{
        $this
->loadLayout();    
        
$cartBlock $this->getLayout()->getBlock('cart_sidebar');
        
        
$ajaxResponse['subTotal'=  $cartBlock->getSubtotal();
        
$ajaxResponse['totalItems'$cartBlock->getSummaryCount();
        
$ajaxResponse['messages'$this->_getSession()->getMessages();
        
$ajaxResponse['cartItemsHtml'"";
        
        
$itemsCollection $cartBlock->getItems();
        
        foreach (
$itemsCollection as $item{
            $ajaxResponse[
'cartItemsHtml'].= htmlentities($cartBlock->getItemHtml($item),ENT_NOQUOTES,"UTF-8");
        
}
        $this
->getResponse()->setBody(Zend_Json::encode($ajaxResponse));
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
moesian
Jr. Member
 
Total Posts:  18
Joined:  2009-10-05
 

This works, though it’s probably not the best way to do this.

protected function _sendJsonCart()
    
{
        $cart 
$this->_getCart();
    
        
$this->loadLayout();    
        
$cartBlock $this->getLayout()->getBlock('cart_sidebar');
        
        
$ajaxResponse['cartItemsHtml'"";
        
        
$itemsCollection $cartBlock->getItems();
        
        foreach (
$itemsCollection as $item{
            $ajaxResponse[
'cartItemsHtml'].= htmlentities($cartBlock->getItemHtml($item),ENT_NOQUOTES,"UTF-8");
        
}
        
        $ajaxResponse[
'subTotal'=  $cartBlock->getSubtotal();
        
$ajaxResponse['totalItems'$cartBlock->getSummaryCount();


        
$storageAreas[] Mage::getSingleton('checkout/session');
        
//$storageAreas[] = Mage::getSingleton('catalog/session');
        
$messageBlock $this->getLayout()->getMessagesBlock();
        foreach (
$storageAreas as $storage){
            $messageBlock
->addMessages($storage->getMessages(true));
            foreach (
$messageBlock->getMessages() as $message{
                $ajaxResponse[
'messages'][$message->getType()][] $messageBlock->htmlEscape($message->getText());
            
}
        }
        $this
->getResponse()->setBody(Zend_Json::encode($ajaxResponse));
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Imagetag
Jr. Member
 
Total Posts:  9
Joined:  2009-09-19
 

Sorry for the late reply, catched a cold.

My approach was a bit different, because I stopped magento from adding the message to the session stack at all, if an ajax request was made.

$message $this->__('%s was successfully added to your shopping cart.'$product->getName());
            if (!
$this->_getSession()->getNoCartRedirect(true)) {
                
if (!$this->ajax$this->_getSession()->addSuccess($message);
                
$this->_goBack();
            
}

Later in the script I symply returned the $message var as part of my json array.

There’s maybe a better solution, too, but i started with Magento just 3 weeks ago, and sometimes it’s pretty difficult to understand and to keep all functions and methods in mind.

 
Magento Community Magento Community
Magento Community
Magento Community
 
magextend
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2009-09-15
France Lyon
 
ShopGuy - 12 October 2009 09:22 AM

Mage Extend,

When you add an item to the cart a popup shows asking if they want to be redirected to their cart or continue shopping. I have a few questions for you:

1) Is the time the popup auto closes adjustable? (3 seconds, 5 seconds, etc, unlimited seconds)

2) Can the popup be configured in the admin to say something when adding to wishlist or compare (goto wishlist or continue browsing, etc)

ShopGuy,

1) Yes you can choose the number of secondsin the admin (no unlimited option but you can set a very hudge number)

2) The popup doesn’t shows up after adding to wishlist/compare, but we can sell you a modified version with these options if you want.

For more informations you can contact us by mail at

Regards,

Adrien from Magextend.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top