如何从外部站点将产品添加到OpenCart? [英] How do I add a product to OpenCart from an external site?

查看:65
本文介绍了如何从外部站点将产品添加到OpenCart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从网站到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="http://***.com/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>

推荐答案

表单操作应为

http://***.com/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: 'http://.../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 = 'http://.../index.php?route=checkout/cart';
            }
        });
    });
});

这篇关于如何从外部站点将产品添加到OpenCart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆