Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Updating shopping cart with jquery


Recommended Posts

I've been looking at the currently existing ajax update cart addons, but all of them seems complicated solutions. So I've been working on one that controls stock with jqery that's pretty simple:

A couple of +/- buttons controls themselves with javascript if a product quantity can be raised or not, disabling the '+' button if there's not enough stock and throwing a warning. Then it just posts the form to a dummy file that just contents application_top to update the cart (I've tried to use application_top directly for the form action but it doesn't work). I plan to add the same checks for the input box so you can't buy more products than the ones in stock, and also change cart quantities inside the navbar module.

This way you don't need to load any content in the page, because the input control is updated with the buttons.

Now the questions are:

  • Do you see any cons for this method? The only one I see is if another customer purchases a product at the same time, so the stock the page uses is not correct, so in checkout you can be redirected to cart again(i think, still not tried). But I may be missing something.
  • How can I update the cart without even loading complete application_top? What should I include in the dummy file?
Link to comment
Share on other sites

@@wHiTeHaT thank you. I was thinking on loading just the needed classes instead of loading application_top (doing a light application_top file as the page won't display at all) as it won't need a lot of things (compatibility layer, mime/email, sef urls, special, template and so on). But with other functions/classes I'm not sure what's strictly necessary and what not.

 

For counting max products I was using a custom property on form controls this way:

      $button_plus = tep_draw_button(NULL, 'fa fa-plus', NULL, NULL,
                                     array('type'=>'button',
                                                   'params' => 'id="plus_'. $products[$i]['id'] .'"' .
                                                   ' max-value= "' . $products_stock . '"'
                                           ), 'btn-info cart-plus') ;

but the problem I see is the check is being done on user's machine, and the max value can change on server while reviewing cart.

Link to comment
Share on other sites

@@wHiTeHaT I know about the application_top. I just was wondering how it works each piece. I can't use the standard form post in shopping cart because the action will do a redirect and the page will reload. I'll have to use a different  action and process it in the dummy file.

 

I'll test the process in-depth to see its weak points, but I think it will work as usual. Maybe I could do an extra check for stock in the dummy but this makes process much more complex and if it works without that check it won't be necessary.

Link to comment
Share on other sites

  • 2 weeks later...

I'm at the point of updating content of the navbar shopping cart (just need the shopping cart module contents). I suppose best the way to do it should be calling the nb_shopping_cart module directly but its getOutput method adds the results to oscTemplate instead of outputting the results. The other way I imagine is including the cm_navbar directly but it sounds less "standard".

 

I've tried:

  include ("includes/modules/content/navigation/cm_navbar.php");
  $navbar = new cm_navbar();
  $navbar->execute();

But it doesn't work. Any help with this?

Link to comment
Share on other sites

Nevermind, it seems much quicker to include the template directly so no osctemplate calls are involved. This does the task perfectly:

  include ("includes/languages/" . $language . "/modules/navbar_modules/nb_shopping_cart.php");
  include ("includes/modules/navbar_modules/templates/shopping_cart.php");
Link to comment
Share on other sites

Well... using template works for navbar, but not for boxes as these have code in class file. I'm now trying to echo the active boxes but had no luck. I was thinking these could be called by:

  $oscTemplate->buildBlocks('boxes');
  echo $oscTemplate->getContent('boxes');

But it doesn't work. What am I doing wrong??

Link to comment
Share on other sites

@@piernas

oscommerce uses code seqvencies structure so every part of the code should be run before display something. This is mean that you can not run parts of codes directly. But fortunately Ajax calls enable to us update something on a rendered page in browsers.
Ajax need a request and a response to do something. Request could be a posted variables to your php codes where not included template_top and template_botton only need the processor parts of codes without all printing code parts.
Unfortunately oscommerce now consists of non-separated rendering and processing code parts so you have to write your own request directly which you want to display. Dont forget that template_top initialize boxes and another modules so you have to solve this problem in your code before. The calculated results coud be rendering with ajax callback/return finaly.
Dont forget to update user $_SESSION too and do something with session missmatches. Ajax call is working like another user with another session even if you push oscID to the action link.

I suggest you make own shopping cart box module to use because another modules could be different and would not work with your special codes together.


use this:
 

echo $oscTemplate->getBlocks('boxes_column_left');

getContent means another functions.

:blink:
osCommerce based shop owner with minimal design and focused on background works. When the less is more.
Email managment with tracking pixel, package managment for shipping, stock management, warehouse managment with bar code reader, parcel shops management on 3000 pickup points without local store.

Link to comment
Share on other sites

@@Gergely thanks for your answer. What you mention is exactly what I am doing, I use an ajax request to a dummy page (that only includes application top and custom code to return the changed values). The page looks likee this:

<?php
/*
 Dummy file for cart - called from shopping_cart.php
 located in ext/modules/shopping_cart/dummy.php
*/

chdir ("../../../");
include ('includes/application_top.php');
include ('includes/languages/' . $language . '/shopping_cart.php');

 echo "<!DOCTYPE html>" ."\n";
 echo '<meta charset="utf-8">' ."\n";

 switch ($_GET['action']) {
   case 'update_product_ajax' :
   // here goes the code for processing the POST request
   // [...]
   break;
 }

// Here goes the code to update the shopping cart in the navigation bar:
  echo '<div id="replacementcart">';
  include ("includes/languages/" . $language . "/modules/navbar_modules/nb_shopping_cart.php");
  include ("includes/modules/navbar_modules/templates/shopping_cart.php");
  echo '</div><br>' . "\n";("includes/languages/" . $language . "/modules/navbar_modules/nb_shopping_cart.php");

// Here goes the code to update the cart sub-totals:
?>
<div id="replacementsubtotal"><p id="cart_subtotal" class="text-right"><strong><?php echo SUB_TITLE_SUB_TOTAL ?> <?php echo $currencies->format($cart->show_total()); ?></strong></p></div>
<span><span id="price_<?=$_POST['updated_product'] ?>"><strong><?= $updated_product_total ?></strong></span></span>
<?php

// Here should go the code to update the cart box on columns - currently not working
  $oscTemplate->buildBlocks('boxes');
  echo $oscTemplate->getContent('boxes');

In the last part I'm trying to echo the whole installed boxes (I'd prefer to echo just the shopping cart box but I've started here) but it doesn't work. Session, cart and other variables are already there.

Curiously, if I previously include the navbar then my code does the job, but I still didn't find why :mellow:

 

The reason of using the standard cart box is the same as I used the navbar template: you don't need to apply changes in both places, you just reload the standard module. If I create an own box, if the box template is updated the new box must be updated, too. Using the current box avoids this problem.

Link to comment
Share on other sites

  • 10 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...