Giter VIP home page Giter VIP logo

Comments (5)

pjedrzejewski avatar pjedrzejewski commented on May 22, 2024

@albanR I have an idea to make the actions ajax friendly, can you show me your routing and action?

from sylius.

albanR avatar albanR commented on May 22, 2024

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.

sogos avatar sogos commented on May 22, 2024

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.

pjedrzejewski avatar pjedrzejewski commented on May 22, 2024

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.

sogos avatar sogos commented on May 22, 2024

Would be great to return the serialized
Cart, that´s true :)

from sylius.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.