allow to create new cart

This commit is contained in:
Manuel Raynaud
2013-07-25 10:06:11 +02:00
parent 72e9c13967
commit 652ce6a6fc
2 changed files with 34 additions and 3 deletions

View File

@@ -26,6 +26,10 @@ namespace Thelia\Action;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Model\CartQuery;
use Thelia\Model\Cart as CartModel;
use Thelia\Model\Customer;
class Cart implements EventSubscriberInterface
@@ -94,6 +98,10 @@ class Cart implements EventSubscriberInterface
public function getCart(Request $request)
{
if (null !== $cartId = $request->getSession()->getCart()){
$cart = CartQuery::create()->findPk($cartId);
}
if ($request->cookies->has("thelia_cart")) {
//le cookie de panier existe, on le récupère
$cookie = $request->cookies->get("thelia_cart");
@@ -117,19 +125,30 @@ class Cart implements EventSubscriberInterface
}
} else {
$cart = $this->createCart();
$cart = $this->createCart($request->getSession());
}
} else {
//le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base.
$cart = $this->createCart();
$cart = $this->createCart($request->getSession());
}
return $cart;
}
public function createCart()
public function createCart(Session $session)
{
$cart = new CartModel();
$cart->setToken($this->generateCookie());
if(null !== $customer = $session->getCustomerUser()) {
$cart->setCustomer($customer);
}
$cart->save();
$session->setCart($cart->getId());
return $cart;
}

View File

@@ -113,4 +113,16 @@ class Session extends BaseSession {
return $this->get('return_to_url', URL::getIndexPage());
}
// -- Cart ------------------------------------------------------------------
public function getCart()
{
return $this->get("cart_id");
}
public function setCart($cart_id)
{
$this->set("cart_id", $cart_id);
}
}