【问题标题】:codeigniter update quantity not workingcodeigniter 更新数量不起作用
【发布时间】:2018-05-24 08:07:44
【问题描述】:

数量不只更新如果我将具有完全相同选项的商品多次添加到购物车中,它将替换而不是增加现有商品的数量 这是购物车视图代码

`<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');
}

当添加产品或更改其数量时一切正常,但如果我将具有完全相同选项的商品多次添加到我的购物车,它会替换而不是增加现有商品的数量

【问题讨论】:

  • 您是否遇到了一些错误?
  • 再次添加产品时没有错误,数量仍为 1
  • 你只是插入数据吗? $this-&gt;cart-&gt;insert($data);
  • 是的,所有代码都可以正常工作 $this->cart->insert($data);但是通过条形码再次添加产品时数量没有增加
  • 您应该使用update,但您正在使用insert

标签: codeigniter


【解决方案1】:

你试过了吗?

<?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 但是当我再次添加相同的产品然后数量没有增加它仍然是 1
  • 我觉得问题出在form_input()上,能不能用普通的html写或者把普通的html输入改成表单输入?
  • 我现在更新代码请解决我尝试近一周但没有成功的问题
  • 您是否尝试过再次运行它?代码更改后?
  • foreach($this->cart->contents() as $item){ if($item['id'] == 'your_product_id'){ $item['qty'] = 3 ; $this->cart->update($item); } }
【解决方案2】:

检查更新查询触发时生成的查询,我认为您的查询中添加了任何信号引用。 要检查最后一个数据库查询,请使用此功能:-
print_r($this->db->last_query());

【讨论】:

  • SELECT tbl_product.*, tbl_inventory.product_quantity, tbl_inventory.notify_quantity FROM (tbl_product) 左连接tbl_inventory ON tbl_inventory.product_id = tbl_product.@9876754326HER@9876754326 @.status = 1 ORDER BY tbl_product.product_id DESC 这是生成的
  • 你好 shoaib,我只是想看看当你触发更新查询时会生成什么查询。
  • 在我的公共函数 add_cart_item_by_barcode 中没有更新查询我永远不明白如果商品已经在购物车中,我将如何使用它如何在公共函数 add_cart_item_by_barcode 中更新其数量所有代码工作正常但不知道那如何在公共函数 add_cart_item_by_barcode 中进行更新查询。
【解决方案3】:

经过一番努力,我得到了正确的代码,这里是插入和更新的正确代码

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');
                      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2022-01-24
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多