2017-03-31 43 views
0

我按照教程here,能夠改變重定向路線。但是,我希望這完全是用Ajax來完成的,而購物車總計會自動更新。但即時通訊在sylius中供應商目錄中的javascript無法通過。有沒有人設法使用純粹的Ajax做到這一點?使用Ajax在Sylius中添加到購物車

我按照Sylius的食譜文檔改變重定向後,添加到購物車詳細在這裏。其中創建重定向類返回一個新的重定向路徑:

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

阿賈克斯已經被用於添加到購物車和重定向發生之後。我試着改變這個代碼來返回一個簡單的回覆('Success',200)。

但是,這會導致供應商目錄中處理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 

該指南告訴我在前端處理這個問題,但我不完全確定如何在vendor目錄中訪問此腳本。該腳本是位於ShopBundle中的sylus-add-to-cart.js。

親切的問候

亞倫

+0

嘿。您應該更深入地詳細說明您的意思,即「在sylius中的供應商目錄中跨越javascript問題」。你有錯誤信息嗎?你改變了什麼代碼? –

+1

嗨,我更新了OP。謝謝。 –

回答

3

我加入一些自定義的路由到的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 

我然後加入所用的形式選擇器上創建和提交處理在前端的響應Ajax請求。

希望這可以幫助任何人。

相關問題