Welcome Guest, Not a member yet? Register   Sign In
flexi cart - A comprehensive shopping cart library for CodeIgniter
#45

[eluser]haseydesign[/eluser]
Hey PoetaWD,

It looks like the problem is because the carts browser session has not been refreshed once the items have been added to the cart.

Due to the way that CodeIgniter sets its session data (Which holds the cart data), the new items added to the cart are only available once the webpage has been redirected.

Since I think its probably a good example that should be available in the demo, I will try and include examples of adding items to the cart via ajax in the next week or so.

However, if you want a pointer in the meantime, this will hopefully solve your problem.

HTML
Code:
<input type="button" value="Buy Item" class="add_item" data-url="http://www.website.com/controller_name/method_name/101"/>

JavaScript Ajax Code
Code:
$('.add_item').click(function(event)
{
   // Get the url of the controller and method that will insert the item to the cart.
   var ajax_url = $(this).attr('data-url');

   // #cart_summary_div is an example name of the div element you want to update with new cart data.
   $('#cart_summary_div').load(ajax_url+' #cart_summary_div');
}

CodeIgniter Controller (In this example called 'controller_name')
Code:
function method_name($item_id = 0)
{
   // Load flexi cart library (if not already)
   $this->load->library('flexi_cart');

   // Compile item data.
   $cart_data = array('id' => $item_id, 'name' => 'Item #'.$item_id, 'quantity' => 1, 'price' => 30);

   // Insert item to cart via flexi cart function.
   $this->flexi_cart->insert_items($cart_data);  

   // Redirect the ajax request to refresh the session data.
   // When called via ajax, the browser will not redirect, instead the default view/data returned
   // by the controller url will be returned to the ajax request.
   redirect('controller_name/method_name');
   exit;
}

Hope that helps you out for now.


Messages In This Thread
flexi cart - A comprehensive shopping cart library for CodeIgniter - by El Forum - 09-06-2012, 03:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB