数量不仅仅更新如果我在购物车中多次添加具有完全相同选项的商品,它会替换而不是增加现有商品的数量 这是购物车查看代码
`<table class="table table-bordered table-hover">
<thead ><!-- Table head -->
<tr>
<th class="active">Sl</th>
<th class="active col-sm-4">Product</th>
<th class="active col-sm-2">Real Price</th>
<th class="active ">Qty</th>
<th class="active ">Disc Price</th>
<th class="active">Total</th>
<th class="active">Action</th>
</tr>
</thead><!-- / Table head -->
<tbody><!-- / Table body -->
<?php $cart = $this->cart->contents() ;
?>
<?php $counter =1 ; ?>
<?php if (!empty($cart)): foreach ($cart as $item) : ?>
<tr class="custom-tr">
<td class="vertical-td">
<?php echo $counter ?>
</td>
<td class="vertical-td"><?php echo $item['name'] ?></td>
<td class="vertical-td"><?php echo $item['pkprice'] ?></td>
<td class="vertical-td">
<input type="text" name="qty" style="width: 50px" value="<?php echo $item['qty'] ?>" onblur ="order(this);" id="<?php echo 'qty'.$item['rowid'] ?>" class="form-control">
</td>
<td>
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" id="<?php echo 'opt'.$item['rowid'] ?>" onclick="return price_checkbox(this)" name="custom_price"
<?php echo $item['price_option'] == 'custom_price' ? 'checked':'' ?>
data-placement="top" data-toggle="tooltip" data-original-title="Custom Price">
</span>
<input type="text" name="price" value="<?php echo $item['price'] ?>" onblur ="order(this);" id="<?php echo 'pri'.$item['rowid'] ?>" class="form-control"
<?php echo $item['price_option'] == 'custom_price' ? '':'disabled' ?> >
</div>
<input type="hidden" name="product_code" value="<?php echo $item['id'] ?>" id="<?php echo 'code'.$item['rowid'] ?>">
</td>
<td class="vertical-td"><?php echo number_format($item['subtotal'], 2, '.', ',') ?></td>
<td class="vertical-td">
<?php echo btn_delete('admin/order/delete_cart_item/' . $item['rowid']); ?>
</td>
</tr>
<?php
$counter++;
endforeach;
?><!--get all sub category if not this empty-->
<?php else : ?> <!--get error message if this empty-->
<td colspan="6">
<strong>There is no record for display</strong>
</td><!--/ get error message if this empty-->
<?php endif; ?>
</tbody><!-- / Table body -->
`
这是控制器代码
public function add_cart_item_by_barcode(){
$product_code = $this->input->post('barcode', true);
$result = $this->order_model->validate_add_cart_item($product_code);
if($result){
$price = $this->check_product_rate($result->product_id, $qty=1);
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);
$data = array(
'id' => $result->product_code,
'qty' => 1,
'price' => $price,
'buying_price' => $result->buying_price,
'name' => $result->product_name,
'pkprice' => $result->p_price,
'tax' => $tax,
'price_option' => 'general'
);
$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'add');
}
redirect('admin/order/new_order/'.$flag ='add');
}
public function check_product_rate($product_id=null, $qty=null)
{
//tier Price check
$tire_price = $this->order_model->get_tire_price($product_id, $qty);
if($tire_price)
{
return $price = $tire_price->tier_price ;
}
//special offer check
$this->tbl_special_offer('special_offer_id');
$offer_price = $this->global_model->get_by(array("product_id"=>$product_id), true);
if(!empty($offer_price)) {
$today = strtotime(date('Y-m-d'));
$start_date = strtotime($offer_price->start_date);
$end_date = strtotime($offer_price->end_date);
if (($today >= $start_date) && ($today <= $end_date)) {
return $price = $offer_price->offer_price;
}
}
//return regular rate
$this->tbl_product_price('product_price_id');
$general_price = $this->global_model->get_by(array("product_id"=>$product_id), true);
return $product_price = $general_price->selling_price;
}
/*** Product tax calculation ***/
public function product_tax_calculate($tax_id, $qty ,$price)
{
$this->tbl_tax('tax_id');
$tax = $this->global_model->get_by(array('tax_id'=>$tax_id), true);
//1 = tax in %
//2 = Fixed tax Rate
if($tax){
if($tax->tax_type == 1)
{
$subtotal = $price * $qty;
$product_tax = $tax->tax_rate * ($subtotal / 100);
//return $result = round($product_tax, 2);
return $result = $product_tax;
}else
{
//$product_tax = $tax->tax_rate * $qty;
$product_tax = $tax->tax_rate * $qty;
return $result = $product_tax;
}
}
}
/*** Update Product Cart ***/
public function update_cart_item()
{
$rowid = $this->input->post('rowid');
$qty = $this->input->post('qty');
$product_price = $this->input->post('price');
$product_code = $this->input->post('product_code');
$custom_price = $this->input->post('custom_price');
if($qty !=0 )
{
//tbl product
$this->tbl_product('product_id');
$result = $this->global_model->get_by(array('product_code'=> $product_code ), true);
//product Inventory Check
$this->tbl_inventory('inventory_id');
$product_inventory = $this->global_model->get_by(array('product_id'=> $result->product_id ), true);
if($qty > $product_inventory->product_quantity)
{
$type = 'error';
$message = 'Sorry! This product has not enough stock.';
set_message($type, $message);
echo 'false';
return;
}
if($custom_price == "on")
{
$price = $product_price;
$price_option = 'custom_price';
}
else
{
//product price check
$price = $this->check_product_rate($result->product_id, $qty);
$price_option = 'general';
}
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty, $price);
$data = array(
'rowid' => $rowid,
'qty' => $qty,
'price' => $price,
'tax' => $tax,
'price_option' => $price_option
);
}else
{
$data = array(
'rowid' => $rowid,
'qty' => $qty,
);
}
$this->cart->update($data);
if($this->input->post('ajax') != '1'){
redirect('admin/order/new_order'); // If javascript is not enabled, reload the page with new data
}else{
echo 'true'; // If javascript is enabled, return true, so the cart gets updated
}
}
/*** Show cart ***/
function show_cart(){
$this->load->view('admin/order/cart/cart');
}
/*** cart Summery ***/
function show_cart_summary(){
$this->load->view('admin/order/cart/cart_summary');
}
/*** Delete Cart Item ***/
public function delete_cart_item($id)
{
$data = array(
'rowid' => $id,
'qty' => 0,
);
$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'delete');
redirect('admin/order/new_order/'.$flag ='delete');
}
这在添加产品或改变其数量完全正常工作时工作正常但是如果我在购物车中多次添加具有完全相同选项的商品,则会替换而不是增加现有商品的数量
答案 0 :(得分:0)
你试过这个吗?
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($counter.'[rowid]', $items['rowid']); ?> // write this
然后这个
<td><?php echo form_input(array('name' => $counter.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
而不是 -
<?php echo form_input(array('name' =>'rowid1[]', 'type'=>'text', 'value' => $items['rowid'], 'maxlength' => '3', 'size' => '5')); ?>
我希望这有效
答案 1 :(得分:0)
检查更新查询触发时生成的查询,我认为在查询中添加了任何信号引用。
检查最后一次数据库查询是否使用此功能: -
的print_r($这 - &GT; DB-&GT; last_query());
答案 2 :(得分:0)
经过多次努力,我正确地得到了我的代码,这是正确的插入和更新代码
function add_cart_item_by_barcode(){
$product_code = $this->input->post('barcode', true);
$result = $this->order_model->validate_add_cart_item($product_code);
$rowid = $this->input->post('rowid');
$cart = $this->cart->contents();
foreach ($cart as $cart) {
if($product_code == $cart['id']){
$rowid=$cart['rowid'];
$qty=$cart['qty'];
$data=array(
'rowid'=>$rowid,
'qty'=>$qty+1
);
$data=$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'add');
redirect('admin/order/new_order/'.$flag ='add');
}
}
if ($result) {
// update rate
$price = $this->check_product_rate($result->product_id, $qty=1);
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);
$data = array(
'id' => $result->product_code,
'qty' => $qty,
'price' => $price,
'buying_price' => $result->buying_price,
'name' => $result->product_name,
'pkprice' => $result->p_price,
'tax' => $tax,
'price_option' => 'general'
);
$this->cart->insert($data);
$this->session->set_flashdata('cart_msg', 'add');
}
redirect('admin/order/new_order/'.$flag ='add');
}