كيفيه زياده سله المشتريات عند الضغظ الزر

ammar • منذ 7 سنوات

اعمل على موقع مثل سوق.كوم المشكله عندما اضغط على add to cart  ماتزداد سله المشتريات استخدم مشlaravel 5. 3

طبعا انا اتابع  الدورة في اليويتوب لكن ماعرف وش الغلط https://www.youtube.com/watch?v=L_5BZKe0Ab4&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH&index=10

the Product Controll


public function getAddToCart(Request $request, $id)
{
  $product  =Product::find($id);
  $oldcart = Session::has('cart') ? Session::get('cart') : null;
  $cart = new Cart($oldcart);
  $cart->add($product , $product->id);

  $request->session()->put('cart',$cart);
// TO show it  dd($request->Session()->get('cart'));
  return redirect()->route('product.index');
}

and the model Cart :


class Cart
 {
 public $items = null;
public $totalQty = 0;
public $totalPrice = 0;

public function __consruct($oldCart){
  if($oldCart){
    $this->$items = $oldCart->items;
    $this->$totalQty  = $oldCart->totalQty;
    $this ->$totalPrice = $oldCart->totalPrice;
  }

}
   public function add($item,$id){
    $storedItem = ['qty' => 0,'price' => $item->price,'item' => $item];
    if ($this->items)
    {
      if(arrary_Key_exists($id,$this->items))
      {
        $storedItem = $this->items[$id];
      }
    }
      $storedItem['qty']++;
      $storedItem['price'] = $item->price * $storedItem['qty'];
      $this->items[$id] = $storedItem;
      $this->totalQty++;
      $this->totalPrice += $item->price;

  }

  }

And this is a Product page:


<a href="#"> <i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
        <span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
      </a>

 

كلمات دليلية:

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (6)

Ali Majrashi • منذ 7 سنوات

هل انت متاكد من خاصية اضافة المنتج للسلة بشكل سليم لانه في الشرح بعد مااضاف خاصية عرض المنتجات قام وضغط على زر Add to Cart لاضافتهم للسلة تاكد ان خاصية اضافة منتج للسلة تعمل بشكل سليم اولا 

ammar • منذ 7 سنوات

هذا قصدك


   array:4 [▼
 "_token" => "NaciZTnMzNzqB02Nvr7RtXJj7c5NtsUF339522l2"
 "_previous" => array:1 [▼
   "url" => "http://localhost:8000"
    ]
  "_flash" => array:2 [▼
"old" => []
"new" => []
]  

Cart {#163 
+items: array:1 [▼
1 => array:3 [▼
  "qty" => 1
  "price" => 20
  "item" => Product {#172 
    #fillable: array:4 [▼
      0 => "imgaePath"
      1 => "title"
      2 => "description"
      3 => "price"
    ]
    #connection: null
    #table: null
    #primaryKey: "id"
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:7 [▼
      "id" => 1
      "created_at" => null
      "updated_at" => null
      "imagePath" => "http://www.abebooks.com/images/books/harry-potter/deathly-hallows.jpg"
      "title" => "hurry potter"
      "description" => "the best book ever"
      "price" => 20
    ]
    #original: array:7 [▼
      "id" => 1
      "created_at" => null
      "updated_at" => null
      "imagePath" => "http://www.abebooks.com/images/books/harry-potter/deathly-hallows.jpg"
      "title" => "hurry potter"
      "description" => "the best book ever"
      "price" => 20
    ]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [▼
      0 => "*"
    ]
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    +exists: true
    +wasRecentlyCreated: false
   }
]
]
+totalQty: 1
+totalPrice: 20
 }

 

Ali Majrashi • منذ 7 سنوات

ياريت اذا ممكن تعمل dd لبيانات السلة المخزنة في الجلسة في View تبع Product page


{{ dd(Session::get('cart')) }}

وتشوف الناتج 

منصور العتيبي • منذ 7 سنوات

اخوي عمار 

جرب تضع


use Illuminate\Support\Facades\Session;

 

ammar • منذ 7 سنوات
24 دقائق مضت, Ali Majrashi said:

{{ dd(Session::get('cart')) }}

طلعلي نفس الي فوق

22 دقائق مضت, منصور العتيبي said:

اخوي عمار 

جرب تضع



use Illuminate\Support\Facades\Session;

 

 نفسها ^_ ^  use Session

Ali Majrashi • منذ 7 سنوات
الان, ammar said:

طلعلي نفس الي فوق

 نفسها ^_ ^  use Session

اذا انت ماشي على نفس الكورس ممكن تعمل Repo على Github عشان اقدر انزل المشروع عندي واختبره واشوف وين المشكلة لانها غريبة تحصل معك 

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !