احمد
3 سال پیش توسط احمد مطرح شد
11 پاسخ

باگ عجیب در سبد خرید

یه مشکل عجیب در سبد خرید پروژه ام رخ داده .
مشکل اینه که وقتی از یه محصول مثلا Redmi Note 9s 64GB دو رنگ مختلف رو به سبد خرید اضافه میکنی بعد میای و یک محصول دیگه مثل POCO M3 رنگ مشکی رو به سبد خرید اضافه میکنی و دوباره اگه بخای یک محصول دیگه رو به سبد خرید اضافه کنی نمیشه و به هیچ وجه محصول بعدی به سبد خرید اضافه نمیشه. تصویر زیر 👇👇

نمیدنم این دیگه چه باگی هست. چند روزه دارم روش کار می کنم .

اینم سورس cartController :

 public function addToCart(Request $request,Product $product, Color $color) {
          // اینجا color همون id رنگی هست که کاربر انتخاب کرده
         $datas = $request->validate([
             'color' => 'required|integer|max:50'
         ],[
            'color.required' => 'لطفا رنگ و گارانتی را انتخاب کنید'
         ]);

         $data = (object)$datas;

           // return Cart::all();
           // return session()->get('cart');

            foreach(Cart::all() as $cart)
            {
               $cart_color = $cart['color'];
            }
           $color_price = $product->colors()->find($datas['color']);

         //  $colors = $color->find($data->color);

           //dd(Cart::all());

         if( Cart::has($product) && $cart_color == $data->color ) {

                    // if(Cart::count($product) < $product->inventory) 
                        //   Cart::update($product , 1);
           alert()->error('محصول مورد نظر از قبل در سبد خرید موجود است')->persistent('بسیار خوب');

           return redirect('/cart');
            }else {
                Cart::put(
                    [
                        'quantity' => 1,
                        'color' => $data->color,
                        'price' => $product->price + $color_price->pivot->pricecolor
                    ],
                    $product
                );
            }

        alert()->success('محصول موردنظر به سبد خرید اضافه شد');
        return redirect('/cart');

        }

اینم کد های cartService :

class CartService
{
    protected $cart;

    public function __construct()
    {
        $cart = session()->get('cart') ?? collect([]);

        $this->cart = $cart->count() ? $cart : collect([
            'items' => [],
            'discount' => null
        ]);

    }

   public function put($value , $obj = null)
   {

        if(! is_null($obj) && $obj instanceof Model)
        {
            $value = array_merge($value, [

                'id' => Str::random(10),
                'subject_id' => $obj->id,
                'subject_type' => get_class($obj),
                'discount_percent' => 0
            ]);

        } elseif(! isset($value['id'])) {
            $value = array_merge($value , [
                'id' => Str::random(10)
            ]);
        }

       // dd(collect($this->cart['items'])->put($value['id'] , $value));
      // dd($this->cart->put($value['id'] , $value));
        $this->cart['items'] = collect($this->cart['items'])->put($value['id'] , $value);
        session()->put('cart', $this->cart);

        return $this;
   }

//این متد برای زمانیکه مثلا  یک محصول دو بار به سبد خرید اضافه بشه تعداد بر تعداد اون محصول در سبد خرید افزوده بشه
   public function update($key , $options)
   {
       $item = collect($this->get($key, false));

       if(is_numeric($options)) {
           $item = $item->merge([
              'quantity' => $item['quantity'] + $options
           ]);
       }

       if(is_array($options)) {
           $item = $item->merge($options);
       }

       $this->put($item->toArray());

       return $this;
   }

   public function count($key)
   {
       if(! $this->has($key) ) return 0;

       return $this->get($key)['quantity'];
   }

   public function has($key)
   {
       if($key instanceof Model) {

        return ! is_null(
            collect($this->cart['items'])->where('subject_id', $key->id)->where('subject_type' , get_class($key))->first()
        );
       }

       return ! is_null(
        collect($this->cart['items'])->firstWhere('id', $key)
       );
   }

   public function get($key , $withRelationShip = true)
   {
        $item = $key instanceof Model
                                ? collect($this->cart['items'])->where('subject_id', $key->id)->where('subject_type' , get_class($key))->first()
                                :  collect($this->cart['items'])->firstWhere('id', $key);

        return $withRelationShip ? $this->withRelationshipIfExist($item) : $item;
   }

   public function delete($key)
   {
        if( $this->has($key) ) {

            $this->cart['items'] = collect($this->cart['items'])->filter(function($item) use ($key){

                if($key instanceof Model) {

                    return ($item['subject_id'] != $key->id ) && ($item['subject_type'] != get_class($key) );

                }

                return $key != $item['id'];

            });

            session()->put('cart' , $this->cart);

            return true;
        }

        return false;
   }

   public function all()
   {
     $cart = $this->cart;

     $cart = collect($this->cart['items'])->map(function($item) use ($cart) {
            $item = $this->withRelationshipIfExist($item);
            $item = $this->checkDiscountValidate($item , $cart['discount']);
            return $item;

      });

      //dd($cart);

     // dd($this->cart);
        return $cart;
   }

   protected function withRelationshipIfExist($item)
   {
        if(isset( $item['subject_id']) && isset($item['subject_type'])) {

            $class = $item['subject_type'];

            $subject = (new $class() )->find( $item['subject_id'] );

            $item[strtolower(class_basename($class))] = $subject;

            unset($item['subject_id']);
            unset($item['subject_type']);

            return $item;

        }

        return $item;
   }

   public function flush()
   {

       $this->cart = collect([
            'items' => [],
            'discount' => null
       ]);

       session()->put('cart', $this->cart);

       session()->forget(['address_id','post_id','shipping_id']);

       return $this;
   }

   public function addDiscount($discount)
   {

       $this->cart['discount'] = $discount;
       session()->put('cart', $this->cart);

   }

   public function getDiscount()
   {
       return Discount::where('code', $this->cart['discount'])->first();
   }

   protected function checkDiscountValidate($item, $discount)
   {

       $discount = Discount::where('code' , $discount)->first();
       if($discount && $discount->expired_at > now() ) {
           if(
               (  ! $discount->products->count() && ! $discount->categories->count() ) ||
               in_array( $item['product']->id , $discount->products->pluck('id')->toArray() ) ||
               array_intersect($item['product']->categories->pluck('id')->toArray(), $discount->categories->pluck('id')->toArray())
           ) {
               $item['discount_percent'] = $discount->percent / 100;
           }
       }

       return $item;
   }

}

ثبت پرسش جدید
احمد
@ahmad316948 3 سال پیش آپدیت شد
حسام موسوی
تخصص : طراح و برنامه نویس
@hesammousavi 3 سال پیش مطرح شد
0

یعنی فقط سه تا محصول اضافه میشه دیگه محصول جدید قرار نمیگیره !؟


Aliakbar Mohajeruni
تخصص : backend php developer
@mohajerun 3 سال پیش مطرح شد
0

این مشکلو داشتم تو کافینگ چک کن رو کوکی ذخیره نمیکنه
اگ نشد یسری کارا دیگ ام کردم این مشکلو حل کردم اعلام کن


ali
@programmer316947 3 سال پیش آپدیت شد
0

@mohajerun
@hesammousavi
سلام. بله بعضی وقت ها هم حتی دو رنگ مختلف از یه محصول که به سبد خرید اضافه بشه دیگه سومی اضافه نمیشه.
در ضمن من تو cartController داخل شرط علاوه بر وجود محصول تکراری رنگ محصول رو‌هم چک کردم.

این مشکلو داشتم تو کافینگ چک کن رو کوکی ذخیره نمیکنه.

من از سشن دارم استفاده می کنم.


Aliakbar Mohajeruni
تخصص : backend php developer
@mohajerun 3 سال پیش مطرح شد
0

محصولات داخل سبد رو در داخل دیتابیس ذخیره کنی مشکل حل میشه
فقط کافیه داخل کافینگ درایور سشن فایلو به دیتابیس تغییر بدید وماگریشنو بزنید داخل داکیومت خود لاراول موجوده


سیدعلی موسوی
تخصص : سی شارپ و پی اچ پی
@juza66 3 سال پیش مطرح شد
0

احتمال میدم توی شرط مشکل دارید

در ضمن من تو cartController داخل شرط علاوه بر وجود محصول تکراری رنگ محصول رو‌هم چک کردم.


احمد
@ahmad316948 3 سال پیش مطرح شد
0

@mohajerun
خوب‌ من بیشتر دنبال علت این مشکل هستم. براچی نباید سشن من به درستی‌کار کنه؟
راهکار تون رو چک می کنم.

@juza66
من حتی اون شرط وجود رنگ‌رو هم‌ برداشتم ولی بازم پرست نشد.


Aliakbar Mohajeruni
تخصص : backend php developer
@mohajerun 3 سال پیش مطرح شد
0

درواقعا وقتی شما لاگین میشد و هرکاری ک دارید با سشن انجام میدید داخل سشن مرورگرتون اتفاق نمیوفته
بلکه داخل لاراول بصورت فایل ذخیره میشه اگ چک کنید متوجه میشید این فایل رو میتونید کد کنید


احمد
@ahmad316948 3 سال پیش مطرح شد
0

@juza66
من اون شرط چک شدن رنگ محصول رو که برداشتم ولی وقتی سه تا محصول به سبد خرید اضافه میکنی اضافه میشه ولی محصول چهارم اضافه نمیشه


احمد
@ahmad316948 3 سال پیش مطرح شد
0

حلش کردم. مشکل از کانفیگ های سشن در فایل .env بود که به این شکل تغییر دادم:

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

سیدعلی موسوی
تخصص : سی شارپ و پی اچ پی
@juza66 3 سال پیش مطرح شد
0

مگه دیفالت فایل env لاراول همین تنظیمات نیست؟!


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام