Solved

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

Posted on 2013-01-06
8
3,599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
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.
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

751 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