Comments (5)
@albanR I have an idea to make the actions ajax friendly, can you show me your routing and action?
from sylius.
Hi, thanks for looking. Here is my actual rooting:
sylius_cart_item_add:
pattern: /add
defaults: { _controller: sylius.controller.cart_item:addAction, type: notAjax }
dp_ajax_add:
pattern: /ajax/add
defaults: { _controller: sylius.controller.cart_item:addAction, type: ajax }
And the corresponding action, purely based on your original one in CartItemController, which only works if part of the original addAction method:
public function addAction(Request $request, $type)
{
$cart = $this->getCurrentCart();
$emptyItem = $this->createNew();
if ($type=="ajax") // this is the altered part, basically replacing flash messages with json response
{
try {
$item = $this->getResolver()->resolve($emptyItem, $request);
} catch (ItemResolvingException $exception) {
$return='{"responseCode" : "400", "message" : "'.$exception->getMessage().'"}';
return new Response($return,200,array('Content-Type'=>'application/json'));
}
$event = new CartItemEvent($cart, $item);
$event->isFresh(true);
$event->isValid(false);
// Update models
$this->dispatchEvent(SyliusCartEvents::ITEM_ADD_INITIALIZE, $event);
$this->dispatchEvent(SyliusCartEvents::CART_SAVE_INITIALIZE, $event);
// Write flash message
// $this->dispatchEvent(SyliusCartEvents::ITEM_ADD_COMPLETED, new FlashEvent());
$ajaxCart = $this->renderView('SyliusWebBundle:Frontend/Cart:ajaxCart.html.twig', array(
'cart' => $cart
));
$return='{"responseCode" : "200", "message" : "ok", "ajaxCart" : '.json_encode($ajaxCart).'}';
return new Response($return,200,array('Content-Type'=>'application/json'));
}
else // unaltered part of the original action
{
try {
$item = $this->getResolver()->resolve($emptyItem, $request);
} catch (ItemResolvingException $exception) {
// Write flash message
$this->dispatchEvent(SyliusCartEvents::ITEM_ADD_ERROR, new FlashEvent($exception->getMessage()));
return $this->redirectToCartSummary();
}
$event = new CartItemEvent($cart, $item);
$event->isFresh(true);
$event->isValid(false);
// Update models
$this->dispatchEvent(SyliusCartEvents::ITEM_ADD_INITIALIZE, $event);
$this->dispatchEvent(SyliusCartEvents::CART_SAVE_INITIALIZE, $event);
// Write flash message
$this->dispatchEvent(SyliusCartEvents::ITEM_ADD_COMPLETED, new FlashEvent());
return $this->redirectToCartSummary();
}
}
from sylius.
Hi we dit it with the same controlle actionr by posting the equivalent form and it worked well.
We will publsh the code today after adjustements.
Be sure to post quantity which is mandatory at validation.
from sylius.
Yes, you should be able to do it with current action. Nevertheless we want it to be more ajax friendly. (Return correct code and do not render the template), as well as action to serialize current cart.
from sylius.
Would be great to return the serialized
Cart, that´s true :)
from sylius.
Related Issues (20)
- [UI] Different product box sizes on the product listing HOT 1
- Form errors during creation of product remove all product images
- [UI] Text going out of the frame in product description HOT 2
- [Refactor][ApiBundle] Unify section resolver usage HOT 1
- Unable to generate url for route sylius_shop_product_show when product slug contains slash HOT 1
- AverageRatingUpdater is called twice when a review is accepted HOT 1
- The complete checkout validation group is difficult to override
- Cannot complete payment via admin
- Api Cart Context calls wrong endpoint method HOT 2
- Invalid constraint configuration in AddProductReview.xml HOT 6
- Catalog promotion is still applying after end date, price is not restored to original HOT 5
- Would be nice to have sylius/promotion not require doctrine/orm
- Order tax adjustment is treated as shipping tax
- products order in the front-end is not showing in descending order ( the latest to the oldest) of the createdAt date only in production. HOT 1
- Symfony error in ResourceBundle HttpFoundationRequestHandler
- As an Admin, I want to resend the confirmation email of an Order that is `fulfilled`
- Incompatibility with SyliusResourceBundle 1.11 HOT 2
- As a Shop API consumer, I want to see the Start and End dates of a Promotion Catalog HOT 2
- As a Shop API consumer, I want to see the current applied catalog promotions of a Product Taxon HOT 1
- Payment request a new way to handle payments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sylius.