Jump to content
sEnte

função update

Recommended Posts

sEnte

Ainda continuando com a mesma app....

No carrinho de compras o que queria fazer é o seguinte, na lista de produtos selecciona-se um produto e faz-se adicionar. Até aqui tudo bem, quando tento adicionar mais que um produto da mesma coisa deveria ser incrementado, mas não é.

atualmente é isto que tenho

<?php
 namespace Cart\Basket;

use Cart\Models\Product;
 use Cart\Support\Storage\Contracts\StorageInterface;
 use Cart\Basket\Exceptions\QuantityExceededException;

class Basket {
protected $storage;
protected $product;
public function __construct(StorageInterface $storage, Product $product) {
  $this->storage = $storage;
  $this->product = $product;
}

public function add(Product $product, $quantity) {
  if ($this->has($product)) {
	$quantity = $this->get($product)['quantity'] + $quantity;
  }
  $this->update($product, $quantity);
}

public function update(Product $product, $quantity) {
  if (!$this->product->find($product->id)->hasStock($quantity)) {
	throw new QuantityExceededException;
  }
  if ($quantity === 0) {
	$this->remove($product);
	return;
  }
  $this->storage->set($product->id, [
	'product_id' => (int) $product->id,
	'quantity' => (int) $quantity,
  ]);
}


"If It Ain't Broke, Break it and build something Cooler!" Unknown

Share this post


Link to post
Share on other sites
HappyHippyHippo

isso depende do funcionamento desta função :

         $this->storage->set($product->id, [
               'product_id' => (int) $product->id,
               'quantity' => (int) $quantity,
         ]);

lembra-te que nunca disseste que biblioteca éssa "Cart"


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
sEnte

à idea é ir à BD buscar os campos referentes à id e a quantidade

foi o nome que dei quando comecei.

{
   "require": {
    "slim/slim": "^3.0",
    "slim/twig-view": "^2.1",
    "php-di/slim-bridge": "^1.0",
    "illuminate/database": "^5.2"
   },

   "autoload": {
  "psr-4": {
    "Cart\\": "app"
  }
   }
}

e estas são as libraries que estou a usar.

Edited by sEnte

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.