Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Sessions, languages and ajax loading


piernas

Recommended Posts

I made an external file that updates the shopping cart without a page reload. It reloads application_top and the needed navbar module and cart totals and replaces the corresponding divs.

 

It works well in every aspect but one: The dummy page loads correctly the session id and cart, but it refreshes the language and takes the default one. I can force to change the language but I don't understand why it keeps the other parameters but changes the language and language_id.

 

Any idea of why it happens?

 

The problem is the component loading sequencies.

 

First you load application_top.php where languages and anithing else defined...

 

Use osCsid when call ajax modules.

 

Url: mydomain/ajax.php?osCsid=orig&language=current_language

 

 

: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

I made an external file that updates the shopping cart without a page reload. It reloads application_top and the needed navbar module and cart totals and replaces the corresponding divs.

 

It works well in every aspect but one: The dummy page loads correctly the session id and cart, but it refreshes the language and takes the default one. I can force to change the language but I don't understand why it keeps the other parameters but changes the language and language_id.

 

Any idea of why it happens?

 

Any chance to see what you have built? I have been trying (without success) to use one of the "old" add to cart/buy now ajax mods  - and then was going to try and get my shopping cart navbar dropdown div to pop open "on event" (clicking the add to cart). In a similar, but less obtrusive way, to the add to cart modal.

Link to comment
Share on other sites

  • 2 weeks later...

@@greasemonkey sorry I missed your message.

 

What I've done for now consists in two parts: The ajax update of the +/- controls and the update of the modules that references the shopping cart. It basically works but I still need to polish the code and review the stock check part.

 

This is the javascript I use:

<script>
  $(document).ready(function() {
  var delete_id = "<?php echo TEXT_DELETE_PRODUCT;?>";

  function formPost(product_id) {
    openModal();
    $('#updated_product').val(product_id);
    $.post($('form[name="cart_quantity"]').attr("action"), $('form[name="cart_quantity"]').serialize(), function(data) {
      $("#navbar-cart").replaceWith($(data).find('#navbar-cart'));
      $("#navbar-full-cart").replaceWith($(data).find('#navbar-full-cart'));
      $("#cart_subtotal").replaceWith($(data).find('#cart_subtotal'));
      $("#btn-cart").replaceWith($(data).find('#btn-cart'));

      product_id = product_id.replace(/{/g, '\\{');
      product_id = product_id.replace(/}/g, '\\}');
      $("#price_" + product_id).replaceWith($(data).find("#price_" + product_id));
      closeModal();
    });
  }

  function deleteProduct(product_id) {
    var formAction = 'shopping_cart.php?products_id=' + product_id + '&action=remove_product'; 
    $('form[name="cart_quantity"]').attr('action', formAction);
    $('form[name="cart_quantity"]').submit();
  }

    $('input[name="cart_quantity[]"]').keydown(function(e) {
      if ($.inArray(e.keyCode, [46, 8, 9, 27]) !== -1 || 
        (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
        (e.keyCode >= 35 && e.keyCode <= 40)) { 
        // let it happen, don't do anything
        return;
      }
      if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
      }
      if ((e.keyCode == 13)) {
        e.preventDefault();
        return false;
      }
    });
    $('input[name="cart_quantity[]"]').keyup(function(e) {

      var product_id = this.id.replace('qty_', ''); 
      var input_pid ='#' + this.id;
      input_pid = input_pid.replace(/{/g, '\\{');
      input_pid = input_pid.replace(/}/g, '\\}');
      plus_pid = input_pid.replace('qty_', 'plus_');

      var quantity_stock = $(plus_pid).attr("max-value");

      if (this.value == "" && quantity_stock >0 ) {
        // Hay al menos una unidad en existencias
        this.value = 1;
      } else if (this.value == "" && quantity_stock <=0 ){
        // TODO: Mensaje eliminar producto y eliminarlo tras aceptar.
        alert (delete_id);
        deleteProduct(product_id);
      }
      if (this.value > quantity_stock) {
        this.value = quantity_stock;
        alert ("<?= TEXT_MAX_QUANTITY ?>");

      }

      formPost(product_id);

      });

    $(".cart-plus").click(function() {

      var pid = $(this).attr("id");
      pid = pid.replace('plus_', '');
      var product_id = pid;
      pid = pid.replace(/{/g, '\\{');
      pid = pid.replace(/}/g, '\\}');
      input_pid = '#qty_' + pid;
      var quantity_stock = $(this).attr("max-value");
      var quantity_asked = parseInt($(input_pid).val()) + 1;

      if (quantity_asked <= quantity_stock) {
        // sumamos 1 ud al carrito
        $(input_pid).val(parseInt($(input_pid).val()) + 1);
        formPost(product_id);

      } else {
        // mensaje "no hay stock suficiente"
        alert("<?= TEXT_MAX_QUANTITY ?>");
      }

    });
    $(".cart-minus").click(function() {
      var product_id = $(this).attr("id").replace('minus_', '');
      pid = product_id.replace(/{/g, '\\{');
      pid = pid.replace(/}/g, '\\}');
      var input_pid = '#qty_' + pid;
      var del_id = "<?php echo TEXT_CONFIRM_DELETE;?>";

      var quantity_stock = $(this).attr("max-value");
      var quantity_asked = parseInt($(input_pid).val()) - 1;

      // Borrar o sustraer cantidad
      if (quantity_asked == 0 && quantity_stock < 1) {
        // codigo para borrar producto
        alert(delete_id);
        deleteProduct(product_id);

      } else if (quantity_asked == 0 && quantity_stock > 0) {
        if (confirm(del_id)) { // pedimos confirmación 
          // codigo para borrar producto
          deleteProduct(product_id);
        }
      } else {
        $(input_pid).val(quantity_asked); // restamos 1
        // actualizamos cesta
        formPost(product_id);
      }
    });
  });

For the total cart and navbar module I use an external dummy file that updates cart values and only loads application_top and the corresponding modules. The script reads those values and replaces the parts of the page that needs to be updated. I use a custom navbar and shopping cart so the code of the file will not be really useful for you, but you need ot put the module's content inside a div and the javascript will replace it. For example, for my cart totals I use on the dummy file:

<div id="replacementsubtotal">
 <p style="font-size: 14px" id="cart_subtotal"  class="label label-success"><strong><?php echo SUB_TITLE_SUB_TOTAL ?> <?php echo $currencies->format($cart->show_total()); ?></strong></p>
</div>
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...