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

[eluser]haseydesign[/eluser]
Hey Rei,

I can give you a generic example of how you can get the price from the database, but this is using an example database schema that will not necessarily fit your requirements, so you will have to adjust it to your needs.

If your not too familiar with ecommerce database schemas, spend a little time researching on Google, as different table structures can have many advantages and disadvantages to each design structure.

Remember, all you essentially need to do is create an array of the products data, and then add that data to the cart via the 'insert_items()' function.
How you get that product data is completely up to you.

Code:
// Get the id of the product option from POST data.
$product_option_id = $this->input->post('product_option_id');

// Run your SQL query to get your product data from the database.
$query = $this->db->select('product_table.product_id, product_table.product_name, product_options.option_name, product_options.price')
->from('product_table')
->join('product_options', 'product_table.product_id = product_options.product_id')
->where('product_option_id', $product_option_id)
->get();

// Check database data was returned.
if ($query->num_rows() == 1)
{
// Assign the database data to a variable.
$product_data = $query->row_array();

// Build the data array that will passed to the flexi cart 'insert_items()' function.
$cart_data = array(
  'id' => $product_data['product_id'],
  'name' => $product_data['product_name'].' - '.$product_data['option_name'],
  'quantity' => 1,
  'price' => $product_data['price']
);

// Insert the product to the cart.
$this->flexi_cart->insert_items($cart_data);
}
#42

[eluser]rei[/eluser]
Thank you very much sir for your very informative sample Smile I'll try to play w/ your library later to be more famliar w/ it. Thanks again sir Smile
#43

[eluser]PoetaWD[/eluser]
EDIT: Looks like this is a CHROME BUG.... :/ ... Have you seing this before ?

Hey man...

There is something wrong when using your library with AJAX... :/

Let me show you:

To manually add a product in the cart:

http://clubemagic.com/cart/addCardToCart/116

(116 is the product ID)

To add a product with AJAX:

http://www.clubemagic.com/site/card/avr/79

(Click on the orange button)

To see the cart array:

http://clubemagic.com/cart

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... :/

Can you help me ?
#44

[eluser]tical[/eluser]
[Comment Removed]
#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.
#46

[eluser]haseydesign[/eluser]
Hey PoetaWD,

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.

You can see a working example of this at http://haseydesign.com/flexi-cart/lite_l...x_examples

The updates are available from the usual Github repo https://github.com/haseydesign/flexi-cart
#47

[eluser]JoJo17[/eluser]
Hi

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:

Code:
public function add_discount()
{

//insert into the discount table
$sql_insert = array(
  'disc_code' => $this->input->post('code') ,
  'disc_description' => $this->input->post('description') ,
  'disc_method_fk' => $this->input->post('code_type') ,
  'disc_value_discounted' => $this->input->post('code_value') ,
  'disc_quantity_required' => '0' ,
  'disc_quantity_discounted' => '0' ,
  'disc_value_required' => '0' ,
  'disc_recursive' => '1' ,
  'disc_non_combinable_discount' => '1' ,
  'disc_void_reward_points' => '0' ,
  'disc_force_ship_discount' => '0' ,
  'disc_valid_date' => $this->common_functions->get_timestamp() ,
  'disc_status' => '1' ,
  'disc_order_by' => '1'
);

$discount_id = $this->flexi_cart_admin->insert_db_discount($sql_insert);

  
}
Code:
$this->input->post('code')
is the code to be entered by the end user to apply their discount to their cart
Code:
$this->input->post('description')
is a description of the discount
Code:
$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
Code:
$this->input->post('code_value')
the amount to discount e.g., 10.00
Code:
$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:

Code:
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!
#48

[eluser]haseydesign[/eluser]
@JoJo17

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.
Code:
UPDATE discounts SET disc_expire_date = DATE_ADD(CURDATE(), interval 1 year);

I will update the Github repo soon with the full SQL script.
#49

[eluser]JoJo17[/eluser]
@haseydesign

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!
#50

[eluser]haseydesign[/eluser]
Hey JoJo,

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'.

Code:
TRUNCATE `discounts`;
INSERT INTO `discounts` VALUES ('1', '2', '12', '1', '0', '0', '0', '0', '0', '10-PERCENT', 'Discount Code \"10-PERCENT\" - 10% off grand total.', '0', '0', '0.00', '10.00', '0', '0', '0', '0', '', '', '', '9998', '2012-05-13 00:00:00', '2015-06-04 00:00:00', '1', '1');
INSERT INTO `discounts` VALUES ('2', '2', '13', '1', '0', '0', '0', '0', '0', '10-FIXED-RATE', 'Discount Code \"10-FIXED-RATE\" - £10 off grand total.', '0', '0', '0.00', '10.00', '0', '0', '0', '0', '', '', '', '9998', '2012-05-13 00:00:00', '2015-06-04 00:00:00', '1', '1');

You can now test the discount codes "10-PERCENT" and "10-FIXED-RATE".

If that doesn't highlight the problem better, can you send me a dump of your discounts table.




Theme © iAndrew 2016 - Forum software by © MyBB