Codeigniter购物车隐身模式

时间:2017-03-11 12:05:21

标签: php codeigniter

我试图探索codeigniter的购物车。我发现了一些我不知道如何解决它的问题。我唯一担心的是,当我试图进入隐身模式并添加到购物车时,它会减去我的库存,当我试图关闭我的隐姓埋名时,股票将无法收回。可能的解决办法是什么?

控制器

    public function validaqty()
{
    $this->form_validation->set_rules('qty','Quantity','callback_checkqty');

    if($this->form_validation->run()==FALSE):
        $validation = array(
            'qty_error' => form_error('qty')
        );
        echo json_encode($validation);
    else:

        $getprod = $this->CrudModel->get_where('products','id',$this->decrypt);
        $cart = array(
            'id'=> secret_url('encrypt',$getprod->id),
            'name' => $getprod->name,
            'qty' => $this->input->post('qty'),
            'pouchsize' => $getprod->pouch_size,
            'subtotal' => $getprod->price * $this->input->post('qty'),
            'weight' => $getprod->weight,
            'lbctype' => $getprod->lbc_type,
            'stock' => $getprod->stock,
            'price' => $getprod->price,
            'image' => $getprod->image,
            'validate_jquery' => 1
        );
        $cartinsert = $this->cart->insert($cart);
        $lessstock = array(
            'stock' => $getprod->stock - $this->input->post('qty')
        );
        $this->db->where('id',$this->decrypt);
        $this->db->update('products',$lessstock);
        echo json_encode($cart);
    endif;
}

MODEL

public function get_where($table,$primary,$id)
{
    $query = $this->db->get_where($table,array($primary=>$id));
    return $query->row();
}

JQUERY / AJAX

 $("#qty_field").on("keypress",function(key){
    if(key.which < 48 || key.which > 57){
        key.preventDefault();
    }
})
$("#qty_form").on('submit',function(){
    var name = $(this).data('name');
    $.ajax({
        url: base_url +"cart/qty",
        type: "POST",
        data: $(this).serialize(),
        success: function(data){
            var result = JSON.parse(data);

            if(result.validate_jquery === 1){

            var name = result.name;
            var qty = result.qty;
            // var subtotal = result.subtotal.toFixed(2);
            var subtotal = result.subtotal;
            var currformat = subtotal.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
            $('#qty_err').html("");
            $('#qty_field').val("1");

            $.notify({
                title: '<strong>ADDED TO CART</strong>'+ "<br>",
                message: "Product Name: "+name +"<br>"+"Quantity: " + qty +"<br>" + "Total: ₱ " +currformat+"<h5>To checkout, click the shopping cart</h5>",

            },

            {
                type: 'pastel-danger',
                delay: 5000,
                template: '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">' +
                    '<span data-notify="title">{1}</span>' +
                    '<span data-notify="message">{2}</span>' +
                '</div>'
            }
            );

            }else{
                $("#qty_err").html(result.qty_error);
            }


        },
        error:function(data){
        alert("Oops... Something went wrong :(");

        }
    });
    event.preventDefault();
});
PS:为了澄清我只想知道是否有一些可能的解决方案来退回股票,如果你试图在浏览器的隐身/私人模式下添加到购物车然后我退出它。而且我看到的另一个问题是,一旦我认为推车的结束,股票也不会返回。

谢谢

0 个答案:

没有答案