product() публичный Метод

public product ( )
Пример #1
0
 public function updateItem($item_form, $add_new = false)
 {
     $quantity = intval(array_get($item_form, 'quantity'));
     $product = Product::find(array_get($item_form, 'product_id'));
     $extraInfo = array_get($item_form, 'extra_info');
     $result = [true, ''];
     if ($product) {
         if ($quantity <= 0) {
             $item = $this->items()->where('product_id', $product->product_id)->get()->first();
             if ($item) {
                 $item->delete();
             }
             if ($this->items()->count() === 0) {
                 $this->delete();
             }
         } else {
             $item = $this->items()->where('product_id', $product->product_id)->get()->first();
             if ($item) {
                 if ($add_new) {
                     $item->quantity += $quantity;
                 } else {
                     $item->quantity = $quantity;
                 }
             } else {
                 $item = new OrderItem();
                 $item->quantity = $quantity;
                 $item->extra_info = $extraInfo;
                 $item->product()->associate($product);
                 if ($product->cost === null) {
                     $item->cost = intval($item_form['cost']);
                 }
             }
             if (!$product->inStock($item->quantity)) {
                 $result = [false, 'not enough stock'];
             } elseif (!$product->enabled) {
                 $result = [false, 'invalid item'];
             } elseif ($item->quantity > $product->max_quantity) {
                 $result = [false, "you can only order {$product->max_quantity} of this item per order. visit your <a href='/store/cart'>shopping cart</a> to confirm your current order"];
             } else {
                 $this->save();
                 $this->items()->save($item);
             }
         }
     } else {
         $result = [false, 'no product'];
     }
     return $result;
 }
Пример #2
0
 public function updateItem($item_form, $add_new = false)
 {
     $quantity = intval($item_form["quantity"]);
     $product = Product::find($item_form["product_id"]);
     $extraInfo = array_key_exists("extra_info", $item_form) ? $item_form["extra_info"] : null;
     $result = [true, ""];
     if ($product) {
         if ($quantity <= 0) {
             $item = $this->items()->where("product_id", $product->product_id)->get()->first();
             if ($item) {
                 $item->delete();
             }
             if ($this->items()->count() === 0) {
                 $this->delete();
             }
         } else {
             $item = $this->items()->where("product_id", $product->product_id)->get()->first();
             if ($item) {
                 if ($add_new) {
                     $item->quantity += $quantity;
                 } else {
                     $item->quantity = $quantity;
                 }
             } else {
                 $item = new OrderItem();
                 $item->quantity = $quantity;
                 $item->extra_info = $extraInfo;
                 $item->product()->associate($product);
                 if ($product->cost === null) {
                     $item->cost = intval($item_form["cost"]);
                 }
             }
             if (!$product->inStock($item->quantity)) {
                 $result = [false, "not enough stock"];
             } elseif ($item->quantity > $product->max_quantity) {
                 $result = [false, "you can only order {$product->max_quantity} of this item per order. visit your <a href='/store/cart'>shopping cart</a> to confirm your current order"];
             } else {
                 $this->save();
                 $this->items()->save($item);
             }
         }
     } else {
         $result = [false, "no product"];
     }
     return $result;
 }