Add anidated products to cart in Sylius


#1

Hi every one, new in the forum here, and this is my first question:

Using Sylius I would like to achieve adding a product to cart with Ajax when user purchase another one.

I tried an ajax call to Sylius ORderItemController, but always return a 500 http error, as tries to make a redirect and I do not wnat to do it.

Here is what i’ve done, is working, but I think must be a better way, as I think this is to mach code for impementing a funcion with should be easier:

Javascript triggers:
Here I pick the user action of clicking on the first product, call via ajax a controller wich will add the product to cart, and then if the response is correct, I call the form wich leads to Sylius method addAction on OrderItemController:

premiumItem.click(function() {
    let formProduct = $("#sylius-product-adding-to-cart");
    let formPremium = $("#sylius-premium-product-adding-to-cart");
    let premiumProductId = formPremium.data('premiumProductId');

    plansbutton.attr('disabled', 'disabled');

    /* Ajax call to custom controller */
    $.ajax({
        url: "{{ path('sylius_shop_ajax_cart_add_item_premium') }}?productId="+premiumProductId,
        method: 'POST',
        dataType: "json",
    })
    .done(function() {
        formProduct.submit(); // If ok, form to Sylius controller is called
    })
    .always(function() {
        plansbutton.removeAttr('disabled');
    });
});

Add product Controller and service:
Here I get the prodcut Id from the request and use a service to get the variant and set the quantity. Once done, the order is updated.

Controller:

/**
* @param Request $request
* @return JsonResponse
*/
public function addPremiumServiceAction(Request $request)
{
    $cartProvider = $this->get("sylius.context.cart");
    try {
        /** @var Order $cart */
        $cart = $cartProvider->getCart();
    } catch (CartNotFoundException $exception) {
        $cart = null;
    }

    if($this->get('opos.sylius_integration.service.order_service')->addProductVariantToCart($cart, $request->get('productId'))) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($cart);
        $em->flush($cart);

        return new JsonResponse(array('result' => 'Order updated'), 200);
    } else {
        return new JsonResponse(array('result' => 'Order could not be updated'), 202);
    }
}

Service:

/**
 * @param Order $cart
 * @param $productId
 * @return mixed
 */
public function addProductVariantToCart(&$cart, $productId)
{
    $productVariant = $this->productVariantRepository->find($productId);

    /** @var OrderItem $orderItem */
    $orderItem = new OrderItem();
    $orderItem->setVariant($productVariant);

    if(!$this->checkRepeatedItemsInCart($cart, $orderItem)) {
        $this->orderItemQuantityModifier->modify($orderItem, 1);
        $this->orderModifier->addToOrder($cart, $orderItem);

        $cart->addItem($orderItem);
    }

    return $cart;
}

This issue is also posted in Stack Overflow, so if any answerer wants the valid answer check here is the link:

Thanks in advance!!