CodeIgniter Forums
Odd Cart Behavior - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Odd Cart Behavior (/showthread.php?tid=52790)

Pages: 1 2


Odd Cart Behavior - El Forum - 06-26-2012

[eluser]RMinor[/eluser]
I am offering free products if certain criteria are met and I am getting the following error when I try to add the free product to the cart "ERROR - 2012-06-26 16:14:13 --> An invalid price was submitted for product ID: 38". I am trying to send the price as 0.00, and i have also tried (float)0.00, and '0.00'. Nothing works. Does anyone have any ideas?


Odd Cart Behavior - El Forum - 06-26-2012

[eluser]InsiteFX[/eluser]
Try .00 if not then you would need to extend the cart class and fix it the current function removes all 0 before the decimal point.


Odd Cart Behavior - El Forum - 06-26-2012

[eluser]RMinor[/eluser]
That did not work. Are there any updates to the cart class that would fix this. I am using version 2.1.0 right now.


Odd Cart Behavior - El Forum - 06-27-2012

[eluser]RMinor[/eluser]
Nothing I have tried thus far has worked. What would I need to do as far as extending the cart class that would enable it to accept 0.00 as a price?


Odd Cart Behavior - El Forum - 06-28-2012

[eluser]Matalina[/eluser]
does 0 not work?

this is what I do:

Code:
$data = array(
       'id'      => $model_id,
       'qty'     => 1,
       'price'   => '0.00',
       'name'    => $model_id
    );



Odd Cart Behavior - El Forum - 06-28-2012

[eluser]RMinor[/eluser]
I had that at first. Here is my add method from my cart controller. Note that in this particular instance the 'free_nail' or 'free_tanktop' attribute will be TRUE, so the price gets reset as 0.00 (I have included comments where it happens). The error logs are telling me that the price is invalid.

Code:
public function add($options_to_check = array('color', 'size', 'free_nail', 'free_tanktop'))
{
    $this->load->model('Cart_model');
    if ($this->input->post('free_nail')) {
        $price = 0.00; // price is set here
        $this->session->set_userdata('free_nail', TRUE);
    } elseif ($this->input->post('free_tanktop')) {
        $price = 0.00; // price is set here
        $this->session->set_userdata('free_tanktop', TRUE);
    } else {
        $price = $this->Cart_model->getProductPrice($this->input->post('id'));
    }
    $data = array(
        'id'    => $this->input->post('id'),
        'name'  => str_replace('/', '-',$this->input->post('name')),
        'qty'   => (int)$this->input->post('quantity'),
        'price' => (float)$price
    );
    $options = array();
    foreach($options_to_check as $option) {
        if($this->input->post($option)) {
            $options[$option] = str_replace('/', '-', $this->input->post($option));
        }
    }
    if(count($options)) {
        $data['options'] = $options;
    }
    if ($this->cart->insert($data)) {
        if (!$this->input->is_ajax_request()) {
            redirect('cart');
        } else {
            $this->output
                ->set_content_type('application/json')
                ->set_output(json_encode(array('message' => 'TRUE', 'data' => $data)));
        }
    } else {
        if ($this->input->is_ajax_request()) {
            $this->output
                ->set_content_type('application/json')
                ->set_output(json_encode(array('message' => 'FALSE', 'data' => $data)));
        }
    }
}



Odd Cart Behavior - El Forum - 06-28-2012

[eluser]Matalina[/eluser]
The difference between my code and your code is:

Code:
$price = '0.00' // a string
$price = 0.00 // a float






Odd Cart Behavior - El Forum - 06-28-2012

[eluser]RMinor[/eluser]
The weird thing is that I had it as '0.00' too and it still would not work.


Odd Cart Behavior - El Forum - 06-28-2012

[eluser]Matalina[/eluser]
You also have:

Code:
'price' => (float)$price



Odd Cart Behavior - El Forum - 06-28-2012

[eluser]RMinor[/eluser]
Well, I put the price variable as a float
Code:
$price = 0.00;
and it still doesn't work. Is there some sort of bug in the cart class? This is really driving me crazy.