简体   繁体   English

使用Ajax添加到Sylius中的购物车

[英]Using Ajax to add to cart in Sylius

I have followed the tutorial here and am able to change the redirect route. 我已经按照这里的教程进行操作并且能够更改重定向路由。 But what i want this to be completed purely with Ajax and for the cart total to be updated automatically. 但是我希望这完全由Ajax完成,并且购物车总额会自动更新。 However im having trouble over-riding the javascript in the vendor directory in sylius. 但是我无法覆盖sylius的供应商目录中的javascript。 Has anyone managed to do this using purely Ajax only? 有没有人设法仅使用Ajax做到这一点?

Ive followed the Sylius cookbook documentation to change the redirect after add to cart detailed here. 在添加到此处详细说明的购物车之后,Ive按照Sylius食谱文档更改了重定向。 The redirect class which is created returns a new redirect route: 创建的重定向类将返回新的重定向路由:

$newUrl = $this->router->generate('your_new_route_name', []); 
$event->setResponse(new RedirectResponse($newUrl));

Ajax is already used to add to cart and the redirect happens afterwards. Ajax已经用于添加到购物车,并且重定向随后发生。 I tried changing this code to return a simple response ('Success', 200). 我尝试更改此代码以返回简单的响应(“成功”,200)。

This however causes an error in the script in the vendor directory which handles the ajax request. 但是,这会在处理ajax请求的供应商目录中的脚本中导致错误。

$.each(response.errors.errors, function (key, message) {
    validationMessage += message;
});

Uncaught TypeError: Cannot read property 'errors' of undefined
  at HTMLFormElement.onFailure (app.js:1363)
  at Object.fail (app.js:23)
  at i (app.js:2)
  at Object.fireWith [as rejectWith] (app.js:2)
  at app.js:23

The guide tells me to handle this on the frontend but im not entirely sure how I can access this script in the vendor directory. 该指南告诉我要在前端处理此问题,但不能完全确定如何在供应商目录中访问此脚本。 The script is the sylus-add-to-cart.js located in the ShopBundle. 该脚本是位于ShopBundle中的sylus-add-to-cart.js。

Kind regards 亲切的问候

Aaron 亚伦

I implemented this by adding some custom routes to the routing.yml file. 我通过将一些自定义路由添加到routing.yml文件来实现这一点。

sylius_shop_partial_cart_add_item_ajax:
    path: /add-item
    methods: [GET]
    defaults:
        _controller: sylius.controller.order_item:addAction
        _sylius:
            template: $template
            factory:
                method: createForProduct
                arguments: [expr:notFoundOnNull(service('sylius.repository.product').find($productId))]
            form:
                type: Sylius\Bundle\CoreBundle\Form\Type\Order\AddToCartType
                options:
                    product: expr:notFoundOnNull(service('sylius.repository.product').find($productId))

sylius_shop_ajax_cart_item_remove_ajax:
    path: /{id}/remove
    methods: [DELETE]
    defaults:
        _controller: sylius.controller.order_item:removeAction
        _format: json

I then added used the form selector to create an ajax request on submit and handle the response on the front end. 然后,我添加了使用表单选择器在Submit上创建ajax请求,并在前端处理响应。

Hope this helps anybody. 希望这对任何人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM