Session::getCart return a Thelia\Model\Cart instance or null if cart is
not valid
This commit is contained in:
@@ -25,12 +25,13 @@ namespace Thelia\Core\HttpFoundation\Session;
|
|||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Session\Session as BaseSession;
|
use Symfony\Component\HttpFoundation\Session\Session as BaseSession;
|
||||||
use Thelia\Core\Security\User\UserInterface;
|
use Thelia\Core\Security\User\UserInterface;
|
||||||
use Thelia\Form\BaseForm;
|
use Thelia\Exception\InvalidCartException;
|
||||||
use Thelia\Model\ConfigQuery;
|
use Thelia\Model\Base\CartQuery;
|
||||||
|
use Thelia\Model\Cart;
|
||||||
use Thelia\Tools\URL;
|
use Thelia\Tools\URL;
|
||||||
|
|
||||||
class Session extends BaseSession {
|
class Session extends BaseSession
|
||||||
|
{
|
||||||
// -- Language ------------------------------------------------------------
|
// -- Language ------------------------------------------------------------
|
||||||
|
|
||||||
public function getLocale()
|
public function getLocale()
|
||||||
@@ -122,7 +123,28 @@ class Session extends BaseSession {
|
|||||||
*/
|
*/
|
||||||
public function getCart()
|
public function getCart()
|
||||||
{
|
{
|
||||||
return $this->get("cart_id");
|
$cart_id = $this->get("cart_id");
|
||||||
|
$cart = null;
|
||||||
|
if ($cart_id) {
|
||||||
|
$cart = CartQuery::create()->findPk($cart_id);
|
||||||
|
try {
|
||||||
|
$this->verifyValidCart($cart);
|
||||||
|
} catch (InvalidCartException $e) {
|
||||||
|
$cart = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $cart;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function verifyValidCart(Cart $cart)
|
||||||
|
{
|
||||||
|
$customer = $this->getCustomerUser();
|
||||||
|
if ($customer && $cart->getCustomerId() != $customer->getId()) {
|
||||||
|
throw new InvalidCartException("customer in session and customer_id in cart are not the same");
|
||||||
|
} else if($customer === null && $cart->getCustomerId() !== null) {
|
||||||
|
throw new InvalidCartException("Customer exists in cart and not in session");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user