未添加到 Codeigniter 购物车的产品
Products not adding to Codeigniter cart
我对 Codeigniter 购物车有疑问。我已经创建了代码并进行了检查,但每次我添加相同的产品时,它只会更新产品的所有信息,但不会添加新的信息。例如,两件完全相同但尺寸或颜色不同的衬衫。我在这里搜索了解决方案,但还没有想出一个。
我的控制器代码
public function addToCart() {
$data = $this->input->post();
$id = $data['id'];
$qty = $data['qty'];
$color = $data['color'];
$cart = $this->cart->contents();
$exists = false;
$rowid = '';
foreach($cart as $item):
if($item['id'] == $id && $item['color'] == $color):
$exists = true;
$rowid = $item['rowid'];
$qty = $item['qty'] + $qty;
else:
// if statement does not equal
endif;
endforeach;
if($exists):
$this->product_model->update_cart_item($rowid, $qty);
redirect('dashboard');
else:
$this->product_model->add_cart_item();
redirect('dashboard');
endif;
}
我的型号代码
public function update_cart_item($rowid, $qty){
$data = array(
'rowid' => $rowid,
'qty' => $qty
);
$this->cart->update($data);
}
public function add_cart_item(){
$id = $this->input->post('id');
$name = $this->input->post('name');
$qty = $this->input->post('qty');
$price = $this->input->post('price');
$color = $this->input->post('color');
$photo = $this->input->post('photo');
$type = $this->input->post('type');
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'color' => $color,
'photo' => $photo,
'type' => $type
);
$this->cart->insert($data);
}
现有产品的更新部分工作正常。不一样的部分是当它是相同的产品但颜色不同时。
由于您想对每个产品执行一些操作,请尝试以下操作:
public function addToCart() {
$data = $this->input->post();
foreach($cart as $item):
$id = $data['id'];
$qty = $data['qty'];
$color = $data['color'];
$cart = $this->cart->contents();
$exists = false;
$rowid = '';
if($item['id'] == $id && $item['color'] == $color):
$exists = true;
$rowid = $item['rowid'];
$qty = $item['qty'] + $qty;
if($exists):
$this->product_model->update_cart_item($rowid, $qty);
else:
$this->product_model->add_cart_item();
endif;
else:
// if statement does not equal
endif;
endforeach;
redirect('dashboard');
}
您想在检查任何下一个产品之前将 $exists
设置为 false。
毕竟,当循环中的所有内容都完成后,您只需要重定向到仪表板。希望有用。
我对 Codeigniter 购物车有疑问。我已经创建了代码并进行了检查,但每次我添加相同的产品时,它只会更新产品的所有信息,但不会添加新的信息。例如,两件完全相同但尺寸或颜色不同的衬衫。我在这里搜索了解决方案,但还没有想出一个。
我的控制器代码
public function addToCart() {
$data = $this->input->post();
$id = $data['id'];
$qty = $data['qty'];
$color = $data['color'];
$cart = $this->cart->contents();
$exists = false;
$rowid = '';
foreach($cart as $item):
if($item['id'] == $id && $item['color'] == $color):
$exists = true;
$rowid = $item['rowid'];
$qty = $item['qty'] + $qty;
else:
// if statement does not equal
endif;
endforeach;
if($exists):
$this->product_model->update_cart_item($rowid, $qty);
redirect('dashboard');
else:
$this->product_model->add_cart_item();
redirect('dashboard');
endif;
}
我的型号代码
public function update_cart_item($rowid, $qty){
$data = array(
'rowid' => $rowid,
'qty' => $qty
);
$this->cart->update($data);
}
public function add_cart_item(){
$id = $this->input->post('id');
$name = $this->input->post('name');
$qty = $this->input->post('qty');
$price = $this->input->post('price');
$color = $this->input->post('color');
$photo = $this->input->post('photo');
$type = $this->input->post('type');
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'color' => $color,
'photo' => $photo,
'type' => $type
);
$this->cart->insert($data);
}
现有产品的更新部分工作正常。不一样的部分是当它是相同的产品但颜色不同时。
由于您想对每个产品执行一些操作,请尝试以下操作:
public function addToCart() {
$data = $this->input->post();
foreach($cart as $item):
$id = $data['id'];
$qty = $data['qty'];
$color = $data['color'];
$cart = $this->cart->contents();
$exists = false;
$rowid = '';
if($item['id'] == $id && $item['color'] == $color):
$exists = true;
$rowid = $item['rowid'];
$qty = $item['qty'] + $qty;
if($exists):
$this->product_model->update_cart_item($rowid, $qty);
else:
$this->product_model->add_cart_item();
endif;
else:
// if statement does not equal
endif;
endforeach;
redirect('dashboard');
}
您想在检查任何下一个产品之前将 $exists
设置为 false。
毕竟,当循环中的所有内容都完成后,您只需要重定向到仪表板。希望有用。