Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-06
8
Medium Priority
?
3,774 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
ID: 38750090
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38751012
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
ID: 38751116
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EddieShipman
ID: 38751454
We wrote a plugin to modify our header. Do you have the capability to do that?
0
 

Author Comment

by:rafique12
ID: 38751484
Sure I can do it...! :-)
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 2000 total points
ID: 38751696
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
ID: 38751787
Ok, I will give it a shot. Thanks pal!
0
 

Author Closing Comment

by:rafique12
ID: 38758415
Nice one!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

782 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