我需要从网站到ocart的产品添加到购物车中.我正在尝试以下代码.用户单击提交按钮后,我得到的只是购物车为空".
I need to add a product to the shopping cart from a website to ocart. I was trying the following code. All I get is "shopping cart empty" after the user clicks the submit button.
我确保产品ID = 40确实存在. thansk寻求任何帮助
I made sure that the product id = 40 does exist. thansk for any help
<form action="***/purchase/index.php?route=checkout/cart" id="personalVirtualPrivateServerForm" method="post"> <input type="hidden" name="product_id" value="40"> <input type="hidden" name="quantity" value="2"> <input type="submit" alt="Order Now" title=" value="Order Now"> </form>推荐答案
表单操作应为
***/purchase/index.php?route=checkout/cart/add mind this particular action being called -^^^^但是我认为这将不起作用,因为称为ControllerCheckoutCart::add()的方法应该与返回JSON响应的AJAX请求一起使用.因此,如果您向该URL提交表单而不显示购物车,它将仅显示JSON响应.
However I think this won't work as the method called ControllerCheckoutCart::add() is expected to work with AJAX request returning the JSON response. So if You submit a form to this URL instead of shopping cart being displayed it will display only the JSON response.
而不是直接提交表单,您应该确保在单击提交"按钮后,该表单是由jQuery AJAX提交的.然后,您可以在成功后将用户重定向到购物车.这是可能的解决方案,请确保填写真实域.未经测试.将此脚本放在存在表单的页面上(假设jQuery已链接到该站点):
Instead of direct submitting the form You should make sure it is submitted by jQuery AJAX after the submit button was clicked. Then You can redirect the user to the shopping cart on success. Here is possible solution, make sure to fill in the real domain. It is not tested. Place this script on the page where the form is present (supposes jQuery is linked to the site):
$(document).ready(function() { $('form#personalVirtualPrivateServerForm input[type="submit"]').click(function(e) { e.preventDefault(); // prevent the form from submitting $.ajax({ type: 'POST', dataType: 'json', url: '.../index.php?route=checkout/cart/add' data: 'product_id=' + $('form#personalVirtualPrivateServerForm input[name="product_id"]').val() + '&quantity=' + $('form#personalVirtualPrivateServerForm input[name="quantity"]').val(), success: function(json) { window.location = '.../index.php?route=checkout/cart'; } }); }); });更多推荐
如何从外部站点将产品添加到OpenCart?
发布评论