Zencart + Ajax made easy (part 3)

As an update to the long delayed series Zencart Ajax Made easy, I have uploaded and make the package available to you all.
First of all, this whole package doesn’t contain any specific javascript plugin itself, it only:

  1. receives the “request” from the client’s browser,
  2. decides whether the request is Ajax request or not
  3. returns content on json format if the request is Ajax.

I also have to apology for the lack of document, I suspect it will be a steep learning step. However, the overall code is relatively short so you may not have much problem with it if you are fluent in php and zencart. If you have any specific question I will be glad to answer them all and perhaps we can build up our documentation that way. Here is a short list of what it can do for you:

  1. It can allow you to break the content into multiple blocks and return just the blocks you want via ajax (this is major performance speed up). The beauty of this method is that it won’t break Zencart default behavior if the users do not have Javascript or do not call the page via javascript

    zencart ajax load

    You can make minimal changes to Zencart current template files to enable AJAX.

  2. Another way to call Ajax is via index_ajax.php, I use this second method for my ajax add to cart module where I let the users submit a form to index_ajax.php and return the new updated cart content.

Download:

Mirror 1 (Megaupload)
Main Link

(Please download from mirror first if possible)

Related posts:

Enjoy this Article ? Bookmark and Share     Follow Us 
  1. deepak says:

    please can u suggest that where can I get a free code for ajax checkout for zencart

    • admin says:

      Our Zencart Ajax Checkout module was released freely to testers some time ago, however the test period has passed now. When new version comes out with major changes we will probably ask for help with testing out the module again.

  2. kclam says:

    Can you show me a client side example to get a block via the zencart ajax pack?

  3. AConfusesOne says:

    kclam
    He can not show you any client side example at all… I’m not so much optimist, and I doubt if this works at all, I read these 3 parts more than 20 times and downloaded the code, went through every thing and still do not know what to do. I even do not know how to call my AJAX JQuery load function, I made many experiments none succeeded. Not a single example is given, not a single demo for a subject like AJAX which is so interesting for all of us to implement in ours carts. This just waste if time, I have already wasted 3 days of my time. The writer of this article either provides working examples more clarifications or just remove it, because he wastes peoples time.

    • admin says:

      This module is being used in many modules such as Ajax Search, Advanced Tab, Ajax Onepage checkout, etc…. on http://eazyecommerce.com/
      In short, it is being used on all our current works which relate to Ajax.

      However, the code shown here on this tutorial is rather outdated and should be considered as a base for your to develop your idea on only. Will the current code ever be released? Not in the near future for some reasons:
      1. This is a rather developers oriented module, and such may require so much time to provide support especially for people not well versed with php and ajax (as is the case with some of our other free modules, which we had to spend much time on support)
      2. The development of this module took much effort and at the moment we don’t want to release it yet.

      However, as we are moving to ZenMagick sometime in the future, you will probably see most of the features of the module incorporated into this cart software.

      • AConfusesOne says:

        Oh yeah? then why you do not answer Kclam’s question, poor him, he has been waiting since December 11, 2009. A client side example to get a block via the zencart ajax pack will a clear answer to our questions and we will be very satisfied and appreciative for that. But -:)

  4. admin says:

    The client side itself is no secret, as mentioned you could just go to the demo of any of our product and peek into the javascript part.

    Please note that we are not required to answer all questions on our blog, especially for the ones that are posted on Sep 2009 and now is almost 2 years later. All code published or shared by us as free code should be used as your own risk. We are not responsible for any damage it may make on your site.

    • AConfusesOne says:

      And now is almost 6 months later Sir not 2 years -:)
      In any case thank you for your efforts although I never could make this run… I hope that in close future we will be able to see some more parts to this documentation like part 4 and 5 with more code demos and explanations.

      Thank you again

  5. Steve says:

    Enough of the whinging, people! They’re doing us a service by even providing help.

    I am trying to implement an ajax contact us based on the code. Downloaded the files and installed them but have a question about usage.

    As far as I can see, the way it works is to define the ids of the blocks of the template that will be returned by ajax in the /includes/modules/pages/contact_us.php file and then replace the zen-redirect with the $Ajax->redirect.

    Here’s how I tried it:
    - I put a div tag around the require($body_code) called mainContent (to be consistent with the naming schemes in the commercial modules referred to) in tpl_main_page and then defined the structure of the page (head,body,mainWrapper,leftColumn,mainContent, rightColumn,bottom) as per the tags in the template being used
    - I then replaced the zen_redirect with the $Ajax->redirect as suggested in the article and then defined the returnblocks to just return mainContent. Not sure why it still redirected with a page refresh. I do have a capture module installed in my contact us page, could that have zen_redirects in it that I need to “catch” too?

    My question is that does every div tag used in the template have to be defined in the array in header.php? Is the general idea that every zen_redirect in the module is changed out with Ajax->redirects?

    • admin says:

      My question is that does every div tag used in the template have to be defined in the array in header.php? Is the general idea that every zen_redirect in the module is changed out with Ajax->redirects?
      –> good question. That is what we did initially, but later we decided that it would be wiser to edit the core code of zencart for this, since you can then avoid changing redirecting code in many places. Below is the code for the latest zencart version, the file to edit is includes/functions/functions_general.php

      function _zen_redirect($url, $httpResponseCode = '') {
          global $request_type;
             
          // Are we loading an SSL page?
          if ( (ENABLE_SSL == true) && ($request_type == 'SSL') ) {
            // yes, but a NONSSL url was supplied
            if (substr($url, 0, strlen(HTTP_SERVER . DIR_WS_CATALOG)) == HTTP_SERVER . DIR_WS_CATALOG) {
              // So, change it to SSL, based on site's configuration for SSL
              $url = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . substr($url, strlen(HTTP_SERVER . DIR_WS_CATALOG));
            }
          }

        // clean up URL before executing it
          while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
          while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
          // header locates should not have the & in the address it breaks things
          while (strstr($url, '&')) $url = str_replace('&', '&', $url);

          if ($httpResponseCode == '') {
            header('Location: ' . $url);
            session_write_close();
          } else {
            header('Location: ' . $url, TRUE, (int)$httpResponseCode);
            session_write_close();
          }

          zen_exit();
        }
       
        function zen_redirect($url, $httpResponseCode=''){
          global $Ajax;
          if(is_object($Ajax))
          $Ajax->redirect($url, $httpResponseCode);
          else
          _zen_redirect($url, $httpResponseCode);
        }

      Let me know if it helps.

  6. Steve says:

    Thank you – will try that out shortly and report back for the benefit of everyone. So what you are saying is that by modifying zen cart core and given you have defined $Ajax in index.php, zen cart always tries to do a redirect via Ajax first and then falls back if it fails?

    In the context of the contact us, won’t this mean the form action for contact_us needs to be called via Ajax too?

    Forgive me if my logic is off, just trying to understand how this works. Lots of php experience, but Ajax is new to me.

    • Steve says:

      I’ve been doing some more work to analyze how this works and I think the following is happening. Can you please confirm or deny?

      - index.php defines two Ajax blocks (defined with $Ajax->startBlock) in index.php – ‘head’,'body’. These correspond to the .. and .. sections.
      - These must be defined in the variable passed to $Ajax->set_structure in the relevant init_includes file
      - You can break the ‘body’ down further by defining an array of div tags that relate to these but you must ALSO call $Ajax->startBlock and $Ajax->endBlock around each of these tags so that the Ajax knows about them in the relevant template file.
      - Finally, you can call $Ajax->setReturnBlocks to define what will be returned from an Ajax call to get the page.

      My question is if all zen_redirect calls are Ajax, how can the cart differentiate between an ajax call (submitting the contact us form) and a non ajax call (first time entering the page)?

      • admin says:

        This is how it will tell the difference:

        function start(){  
                // set status
                if(((isset($_SERVER['HTTP_X_REQUESTED_WITH'])) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') || (isset($_REQUEST['isajaxrequest']) && $_REQUEST['isajaxrequest'] == 1)){
                    $this->status = true;
                   
                    // start buffering
                    ob_start();
                }
                $this->restoreMessage();
            }

        (Taken from our latest Ajax Class)

  1. There are no trackbacks for this post yet.

Leave a Reply

We care about your Privacy. Copyright © 2009 RubikIntegration. Powered by Zen Cart and Wordpress