Solved

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

Posted on 2013-01-06
8
3,526 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding a Javascript click event to an HTML element given a class dynamically 5 38
php convert date format 3 21
PHP and google maps 13 45
Curl & PHP Command Help 4 23
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…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 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