如何解决Codeigniter推车库存控制问题



我将产品添加到购物车中,但当我想检查库存时,我遇到了问题。当我第一次添加产品时,$this->cart->contents()篮在第一阶段是空的,因此产品数量显示为0。当我再次将产品添加到购物篮中时,会显示我添加的旧产品数量。我无法控制库存。你能给我一个如何做到这一点的想法吗?

所以这就是我想要做的;控制篮子里的产品数量和我体内的产品数量,防止添加到篮子里的过程。我现在唯一的问题是他看到篮子里的旧数量。有人帮忙吗?

我显示gif:giphy 的问题

public function add()
{
$response       = array();
$opt_arr        = array();
$urun_id        = $this->input->post('product_id');
$miktar         = $this->input->post('quantity');
$select_opt     = $this->input->post('select_opt');
$radio_opt      = $this->input->post('radio_opt');

$where = $this->yurun_model->urun(array("urun_id" => $urun_id));
$ayarlar = ayarlar();
if (!$where) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Bulunamadı!';
print_r(json_encode($response));
die;
}
$sepet_Say  = [];
$radio_Say  = [];
$select_Say = [];
if ($this->cart->contents()) {
foreach($this->cart->contents() as $items){
if ($items['id']==$urun_id) {
if ($items['options']['radio_opt']==$radio_opt) {
$radio_Say[] = $items['qty'];
}
if ($items['options']['select_opt']==$select_opt) {
$select_Say[] = $items['qty'];
}
if ($where->urun_radio==0 || $where->urun_select==0) {
$urun_gu = $where->urun_stok;
if ($urun_gu==0) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Bu Ürün Stoklarımızda Kalmamıştır!';
print_r(json_encode($response));
die;
}
// if ($urun_gu<=$items['qty']) {
//  $response['durum'] = 'error';
//  $response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$urun_gu.' Adettir';
//  print_r(json_encode($response));
//  die;
// }
}
$sepet_Say[] = $items['qty'];
}
}
$qty_sonuc      = array_sum($sepet_Say);
$radio_sonuc    = array_sum($radio_Say);
$select_sonuc   = array_sum($select_Say);
}else{
$qty_sonuc      = 0;
$radio_sonuc    = 0;
$select_sonuc   = 0;
}
if ($where->urun_ifiyat!='0.00') {
$fiyat = $where->urun_ifiyat;
}else{
$fiyat = $where->urun_fiyat;
}
if ($where->urun_radio>0 || $where->urun_select>0) {
$stok = 0;
}else{
$stok = $where->urun_stok;
}
if ($where->urun_radio!=0) {
if ($where->urun_radio!=0) {
if (!$radio_opt) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Lütfen Ürün Özelliği Seçin!';
print_r(json_encode($response));
die;
}
}
$urun_radio_json  = json_decode($where->urun_radio_json);
$radio_say  = [];
foreach ($urun_radio_json as $radio) {
if ($radio_opt==$radio->id) {
if ($radio->stok<$miktar) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$radio->stok.' Adettir';
print_r(json_encode($response));
die;
}
if ($radio->stok<=$radio_sonuc) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$radio->stok.' Adettir';
print_r(json_encode($response));
die;
}
$radio_say[] = $radio->stok;
}
}
$radio_sayi = array_sum($radio_say);
}else{
$radio_sayi = 0;
}

if ($where->urun_select!=0) {
if (!$select_opt) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Lütfen Ürün Özelliği Seçin!';
print_r(json_encode($response));
die;
}
if ($select_opt==0) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Lütfen Ürün Özelliği Seçin!';
print_r(json_encode($response));
die;
}
$urun_select_json = json_decode($where->urun_select_json);
$select_say = [];
foreach ($urun_select_json as $select) {
if ($select_opt==$select->id) {
if ($select->stok<$miktar) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$select->stok.' Adettir';
print_r(json_encode($response));
die;
}
if ($select->stok<=$select_sonuc) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$select->stok.' Adettir';
print_r(json_encode($response));
die;
}
$select_say[] = $select->stok;
}
}
$select_sayi = array_sum($select_say);
if ($select_sayi!=0) {
}

}else{
$select_sayi = 0;
}
$varyant_stok = $radio_sayi+$select_sayi;
if ($varyant_stok==0) {
$urun_stok = $stok;
}else{
$urun_stok = $varyant_stok;
}
if ($urun_stok==0) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Bu Ürün Stoklarımızda Kalmamıştır!';
print_r(json_encode($response));
die;
}
if ($urun_stok<$miktar) {
$response['durum'] = 'error';
$response['s_baslik'] = 'Ürün Stok Sınırını Geçtiniz! Ürün Stok Sayısı : '.$urun_stok.' Adettir';
print_r(json_encode($response));
die;
}
if ($where->urun_kdv) {
$kdv = $fiyat*$where->urun_kdv/100;
$fiyat_sonuc = $fiyat+$kdv;
}elseif ($ayarlar->urun_kdv) {
$kdv = $fiyat*$ayarlar->urun_kdv/100;
$fiyat_sonuc = $fiyat+$kdv;
}else{
$fiyat_sonuc = $fiyat;
}

$data = array(
"id"            => $where->urun_id,
"name"          => $where->urun_ad,
"qty"           => $miktar,
"price"         => number_format((float)$fiyat_sonuc, 2, '.', ''),
"options"       => array('select_opt' => $select_opt, 'radio_opt' => $radio_opt),
"kupon"         => "",
"indirim"       => 0,
"eski_fiyat"    => "",
"kdvsiz_fiyat"  => $fiyat,
"kdv"           => $where->urun_kdv,
"kargo"         => 0
);
$this->cart->insert($data);
$response['durum'] = 'success';
$response['s_baslik'] = 'Ürün Sepete Eklendi!';
print_r(json_encode($response));
}

我用这种方法解决了这个问题。

if ($select->stok<=$select_sonuc+$miktar) {

最新更新