The problem: When I add the product manually it works… When I add the product using AJAX it doesnt work… The system returns me a success message, but the product doesnt show in the cart list… :/
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.
$('.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’)
function method_name($item_id = 0) { // Load flexi cart library (if not already) $this->load->library('flexi_cart');
// 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; }
I don’t know whether you solved your problem or not, but in any case, I’ve now updated the flexi cart demo with examples of adding items to the cart via Ajax.
This looks to be a great library and just what I need! I am having a little trouble with the discounts though and perhaps I’m not understanding it correctly but I can’t get it working how I want.
I want the discounts to be stored in the database and when a user enters the correct code for the discount it gets updated to their cart. What I have is:
In the admin area of the site I have a specific discounts controller and the part that saves the discount codes to the database in the discounts model is thus:
is the code to be entered by the end user to apply their discount to their cart
$this->input->post('description')
is a description of the discount
$this->input->post('code_type')
the ID of the discount method as from the discount methods table, in my case this will be 7 or 8
$this->input->post('code_value')
the amount to discount e.g., 10.00
$this->common_functions->get_timestamp()
this is just some custom code to insert the current date and time
This does add the discount to the discount table.
Within the shopping cart controller I have a function in the method that checks the entered code against the discount table:
public function promo_code() {
if ($_POST) : $this->flexi = new stdClass; $this->load->library('flexi_cart'); $discount_data = $this->input->post('promo_code'); $code = $this->flexi_cart->update_discount_codes($discount_data); $msg = $this->flexi_cart->get_messages(); endif; echo $msg;
}
but it always returns:
Discount code(s) is invalid.
Discounts were not updated.
even though the code is in the discount table. I just want to be able to set a discount of either a flat fee or percentage on the cart total (excl. shipping).
I’m obviously misunderstanding this somewhere along the line and would appreciate if you could point me in the right direction please!
By the looks of your code you’re doing everything right.
I just tried doing some testing myself and was puzzled why I was getting the exact problem.
What I’m betting on is that you are using the demo sql file, and due to an oversight by myself, the discounts are actually invalid - as in their expiry date has now passed.
If you are using the demo sql file, what you need to do is update the expiry dates of these discounts.
For a quick fix, try running the following sql to update all the demo discounts.
UPDATE discounts SET disc_expire_date = DATE_ADD(CURDATE(), interval 1 year);
I will update the Github repo soon with the full SQL script.
I’m not actually using the demo sql at all, I’m integrating flexi cart into an existing site. I have checked the expiry date of my discount (originally it was 0000-00-00 as I presumed I didn’t have to set one) and have changed it to a date in the future but it still does the same so it must be a problem somewhere else. I have loaded up your demo SQL into a separate database so I can compare the differences!
Have you tried using the example sql discount data within your own existing site?
What I mean by that, is if you insert the example discount data into your sites discount table, they should be valid and prove whether there is either a library config problem, or a database data problem.
To try this, backup/create a copy of your current discount table, and then run the following sql on the table named ‘discounts’.
The usage limit defines how many times the discount can be used, when set to 0, the cart thinks all the discount codes have been used up.
——————————————————
I understand looking at the discount tables is a little daunting with so many columns and options.
To help out in future, I would suggest you create a separate installation of the flexi cart demo so that you can do stuff like build discounts via the provided demo and see what kind of output it does.
Thank you for your help and patience - you are correct that was the problem though I was hoping to be able to have an unlimited use which is why I wasn’t setting it!
The shipping within flexi cart has to be manually defined - there is currently no integration with any shipping companies.
However, that said, there are still many options to allow you to customise shipping rates for many different circumstances.
Shipping rates can be based on:
+ Location / Zone.
+ Order values and weight.
Shipping options include:
+ Different pricing tiers can be defined within each shipping option.
+ Customised tax value - that can differ from the default cart value.
+ Inclusion/Exclusion from cart discounts - i.e. A 10% cart wide discount could be excluded from affecting the shipping value.
If you, or anyone else does go down the road of trying to implement 3rd party shipping rates that can be integrated into the cart library, I’d be happy to lend a hand so the code could be included within the library for others to use. All contributors would be credited.