Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-06
8
Medium Priority
?
3,718 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
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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.

722 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