Using now BaseAction methods in Cartp.php
This commit is contained in:
@@ -31,7 +31,7 @@ use Thelia\Core\Event\ActionEvent;
|
|||||||
|
|
||||||
abstract class BaseAction
|
abstract class BaseAction
|
||||||
{
|
{
|
||||||
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = 'POST')
|
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
|
||||||
{
|
{
|
||||||
$form = $aBaseForm->getForm();
|
$form = $aBaseForm->getForm();
|
||||||
|
|
||||||
|
|||||||
@@ -72,55 +72,50 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
|||||||
*/
|
*/
|
||||||
public function addArticle(ActionEvent $event)
|
public function addArticle(ActionEvent $event)
|
||||||
{
|
{
|
||||||
$request = $event->getRequest();
|
$request = $event->getRequest();
|
||||||
|
|
||||||
$cartAdd = $this->getAddCartForm($request);
|
try {
|
||||||
$form = $cartAdd->getForm();
|
$cartAdd = $this->getAddCartForm($request);
|
||||||
|
|
||||||
$form->bind($request);
|
$form = $this->validateForm($cartAdd);
|
||||||
|
|
||||||
if($form->isValid()) {
|
$cart = $this->getCart($request);
|
||||||
try {
|
$newness = $form->get("newness")->getData();
|
||||||
$cart = $this->getCart($request);
|
$append = $form->get("append")->getData();
|
||||||
$newness = $form->get("newness")->getData();
|
$quantity = $form->get("quantity")->getData();
|
||||||
$append = $form->get("append")->getData();
|
|
||||||
$quantity = $form->get("quantity")->getData();
|
|
||||||
|
|
||||||
$productSaleElementsId = $form->get("product_sale_elements_id")->getData();
|
$productSaleElementsId = $form->get("product_sale_elements_id")->getData();
|
||||||
$productId = $form->get("product")->getData();
|
$productId = $form->get("product")->getData();
|
||||||
|
|
||||||
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
|
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
|
||||||
|
|
||||||
if($cartItem === null || $newness)
|
if($cartItem === null || $newness)
|
||||||
{
|
{
|
||||||
$productPrice = ProductPriceQuery::create()
|
$productPrice = ProductPriceQuery::create()
|
||||||
->filterByProductSaleElementsId($productSaleElementsId)
|
->filterByProductSaleElementsId($productSaleElementsId)
|
||||||
->findOne()
|
->findOne()
|
||||||
;
|
;
|
||||||
|
|
||||||
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
|
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
|
||||||
}
|
|
||||||
|
|
||||||
if($append && $cartItem !== null) {
|
|
||||||
$this->updateQuantity($cartItem, $quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$this->redirect($cartAdd->getSuccessUrl($request->getUriAddingParameters(array("addCart" => 1))));
|
|
||||||
} catch (PropelException $e) {
|
|
||||||
\Thelia\Log\Tlog::getInstance()->error(sptinf("error on adding item to cart with message : %s", $e->getMessage()));
|
|
||||||
$message = "Impossible to add this article to your cart, please try again";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
if($append && $cartItem !== null) {
|
||||||
|
$this->updateQuantity($cartItem, $quantity);
|
||||||
|
}
|
||||||
|
|
||||||
$message = "Missing or invalid data";
|
|
||||||
|
$this->redirect($cartAdd->getSuccessUrl($request->getUriAddingParameters(array("addCart" => 1))));
|
||||||
|
} catch (PropelException $e) {
|
||||||
|
\Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
|
||||||
|
$message = "Failed to add this article to your cart, please try again";
|
||||||
|
}
|
||||||
|
catch(FormValidationException $e) {
|
||||||
|
|
||||||
|
$message = $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cartAdd->setError(true);
|
// The form has errors, propagate it.
|
||||||
$cartAdd->setErrorMessage($message);
|
$this->propagateFormError($cartAdd, $message, $event);
|
||||||
|
|
||||||
$event->setErrorForm($cartAdd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateQuantity(CartItem $cartItem, $quantity)
|
protected function updateQuantity(CartItem $cartItem, $quantity)
|
||||||
|
|||||||
Reference in New Issue
Block a user