Hi guys I'm using codeigniter 3 for a project. Now I need to add a cart system to it. So I'm trying to use CI's default Cart Class for it. But when I try to load it through view it gives me an error message
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$cart
This is my Cart.php which is the controller.
defined('BASEPATH') OR exit('No direct script access allowed');
class Cart extends CI_Controller {
public function __construct (){
parent::__construct();
$this->load->helper(array('url','language','form'));
}
public function index()
{
$this->load->library('cart');
$this->load->view('home/cart');
}
public function update(){
$this->load->library('cart');
$this->cart->update($_POST);
redirect(base_url().'home/cart');
}
public function add_variables(){
$this->load->library('cart');
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
),
array(
'id' => 'sku_567ZYX',
'qty' => 1,
'price' => 9.95,
'name' => 'Coffee Mug'
),
array(
'id' => 'sku_965QRS',
'qty' => 1,
'price' => 29.95,
'name' => 'Shot Glass'
)
);
$this->cart->insert($data);
}
}
and This is my view (cart.php)
<!DOCTYPE html>
<html>
<head>
<title>Cart<title>
</head>
<body>
<?php $this->load->view('home/top-bar'); ?>
<div id="page">
<?php $this->load->view('home/header'); ?>
<div id="center-right">
<h1>This is Cart</h1>
<?php echo form_open(base_url().'Cart/update'); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('', 'Update your Cart'); ?></p>
</div>
</div>
<?php $this->load->view('home/footer'); ?>
</body>
</html>
Could someone give me a help on this? Thank you.
You have to change your controller class name cart to other name because cart class already used that name try to change
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With