codeigniter3 as you can see in the below PHP code I'm trying to redirect the view_cart function but I am struggling to add_to_cart function that I want When I go to the add_to_cart function should be redirected to the 'Shoppycart/view_cart' page but add_to_cart throwing me an error.
Shoppycart.php
public function index(){
$this->load-view('test');
}
public function add_to_cart($product_id) {
$product = $this->Cart_model->get_product_by_id($product_id);
if ($product) {
$cart_item = array(
'id' => $product->id,
'qty' => 1, // You can adjust the quantity as needed
'price' => $product->price,
'name' => $product->name,
'options' => array('image' => $product->image_url)
);
$this->cart->insert($cart_item);
redirect('Shoppycart/view_cart');
} else {
echo "Product not found!";
}
}
public function view_cart(){
$this->load->view('product/cart_view');
}
Cart_model.php
public function get_product_by_id($product_id){
$this->db->where('id', $product_id);
return $this->db->get('products')->row();
}
when I go to this URL http://localhost/webmodify/Shoppycart/add_to_cart/
I got an error message:
Too few arguments to function Shoppycart::add_to_cart(), 0
routes.php
$route['default_controller'] = 'Shoppycart';
$route['Shoppycart/add_to_cart/(:num)'] = 'Shoppycart/add_to_cart/$1';
Retrieve and display cart items and also add an item to the cart
In the function, your parameter is required. If you don't want to pass a parameter, please make sure to declare it as null.
public function add_to_cart($product_id = '')
{
//your code
}