]> _ Git - pmi.git/commitdiff
Prevent extra queries when fetching cart data. WIP #2738 @0.25
authorStephen Cameron <stephen@cubedesigners.com>
Mon, 9 Sep 2019 08:26:48 +0000 (10:26 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Mon, 9 Sep 2019 08:26:48 +0000 (10:26 +0200)
app/Models/Product.php

index 86ae4468f291e877041b96c4491e3f67536f0850..ebf261daa2f49211be5213c69e4bc655fa04679b 100644 (file)
@@ -14,7 +14,7 @@ class Product extends CubistMagicPageModel
     protected static $_specifications = null;
     protected static $_productTypes = null;
     protected static $_specificationFields = null;
-    protected static $_cart_data;
+    protected static $_cart_data = null;
 
     /**
      * @var array
@@ -353,20 +353,23 @@ class Product extends CubistMagicPageModel
         if (!self::$_cart_data) {
 
             $cart_items = session('cart_items', []);
-
             self::$_cart_data = [];
-            $products = self::with('media')->whereIn('id', array_keys($cart_items))->get();
-
-            foreach ($products as $product) {
-                self::$_cart_data[] = [
-                    'id' => $product->id,
-                    'name' => $product->name,
-                    'reference' => $product->reference,
-                    'category' => $product->type->name,
-                    'quantity' => $cart_items[$product->id],
-                    'image' => $product->image,
-                    'URL' => $product->url,
-                ];
+
+            if (count($cart_items) > 0) {
+
+                $products = self::with('media')->whereIn('id', array_keys($cart_items))->get();
+
+                foreach ($products as $product) {
+                    self::$_cart_data[] = [
+                        'id'        => $product->id,
+                        'name'      => $product->name,
+                        'reference' => $product->reference,
+                        'category'  => $product->type->name,
+                        'quantity'  => $cart_items[$product->id],
+                        'image'     => $product->image,
+                        'URL'       => $product->url,
+                    ];
+                }
             }
         }