Solved

Update "My Cart" link using AJAX add to cart Magento

Posted on 2013-01-06
8
3,474 Views
Last Modified: 2016-03-02
Hi peoples,

I have a question regarding AJAX calls in Magento. I've installed an extension which allows products to be added to the shopping cart without refreshing the page and it is working fine for the cart sidebar. However, I also have the number of items displayed in the
"My Cart (4 items)" link in the header which is not being updated until the page is refreshed.

I'm looking for a way to update the toplink too?

If there are any suggestions please let me know or if you would like me to post some code for you to reference...

Thanks in advance!
0
Comment
Question by:rafique12
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
Comment Utility
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I have had a problem with Magento and ajax calls before. It seems that the ajax call does not retrieve the same Magento session as the one it is being called from. I found this out when trying to check the logged in user using ajax.

I don't really have a solution, though.
0
 

Author Comment

by:rafique12
Comment Utility
I have the situation where the cart in the sidebar updates but I'm not sure how to update the header???

Its something I need to solve but can't find a solution either.
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
We wrote a plugin to modify our header. Do you have the capability to do that?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rafique12
Comment Utility
Sure I can do it...! :-)
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
Comment Utility
We use this "template", called usernav.html (located in our default template folder
{exp:embedded_cart site_url = "{site_url}" site_url_https = "{site_url_https}"}

Open in new window

Notice also the 2 global variables, I don't think you really need them.

We load it with this script:
$.ajax({
  url: '/usernav',
  success: function(data) {
  $('.userNav').html(data);
}	

Open in new window


here is out embedded_cart plugin code, save as pi.embedded_cart.php in third_party/embedded_cart directory
<?php
if ( ! defined('BASEPATH')) 
    exit('No direct script access allowed');

require_once('/srv/www/htdocs/store/app/Mage.php');

class Embedded_cart {

    function Embedded_cart() {
        $site_url = 'http://' . $_SERVER['HTTP_HOST'] . '/';
        $site_url_https = 'https://' . $_SERVER['HTTP_HOST'] . '/';
        
        umask(0);
        Mage::app();
        $pidsess = Mage::getSingleton('core/session', array('name'=>'frontend'));
        
        $cart_count = 0;
        Mage::getSingleton('checkout/cart')->getItemsCount();
        $cart = Mage::helper('checkout/cart')->getCart();
        $cart_count = $cart->getItemsCount();

        // Cart now contains the number of items, though it can be null instead of 0,
        // so we should check that.
		
        if ($cart_count > 0) {
            $result = "<span class=\"carticon\">($cart_count)</span><a href=\"{$site_url}store/checkout/cart/\">My Cart</a>\n";
        } else {
            $result = "<span class=\"carticon\">(0)</span><a href=\"{$site_url}store/checkout/cart/\">My Cart</a>\n";
        }    
        
        $magsession = Mage::getSingleton('customer/session');
        if ($magsession->isLoggedIn() == true) {
        	setcookie("loggedin","yes",time()+3600, '/', $_SERVER['HTTP_HOST']);
		$customer = Mage::getSingleton("customer/customer")->load($magsession->getCustomerId()); 
        	$cname = $customer->getName();
        	$result  .= "<span>Welcome, $cname!</span>";
        	$result  .= "<a href=\"{$site_url}store/customer/account/logout/\">Logout</a>\n";
        } else {
            $result  .= "<a href=\"{$site_url_https}login/\">Login</a>\n";
            // We do other things, like remove items from the cart, here due to our business rules
        }
        echo $result;
    }
}

Open in new window


The benefit of using this is that you don't have to worry about the cart counts, it reads them directly.
Now I believe this only works if you reload or redirect but this is something you can test.
0
 

Author Comment

by:rafique12
Comment Utility
Ok, I will give it a shot. Thanks pal!
0
 

Author Closing Comment

by:rafique12
Comment Utility
Nice one!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now