简体   繁体   中英

Link a PHP file to an AJAX request in Magento

I'm a newbie to Magento and I'm using Magento 1.9

I have a template file to a quick order form. As I need to add some custom php coding, I have created a php file under the same folder (/templates/quickorder/QuickOrder.php) .

What I need to implement is, when a user clicks on the "Add to Cart" button, an ajax request should be sent to my QuickOrder.php file and that will add the product to the cart.

So far my js code is as follows:

$(document).ready(function(){

    $("button[name=addtocart]").click(function(){

        var id = $("select[name=product]").val();
        var qty = $("input[name=quantity]").val();
        $.ajax({
                url: 'QuickOrder.php',
                method: 'post',
                data: { 'pro_id': id,
                        'qty':qty 
                },
                success:function(data){

                },
        });

    });

});

And my QuickOrder.php file will accept the values and add the item to the cart.

$cart =Mage::getSingleton('checkout/cart');
$cart->addProduct($product, array('qty'=>$qty)); 
$cart->save();

This is not working. It cannot find the QuickOrder.php file.

My question is, can I specify the correct path to the php file without creating a separate module?

You need to create new module with frontcontroller action for that you can refer this link magento New module action

Then you can give url of that action in your ajax code like

    $.ajax({
            url: 'http://[hostname]/[magento]/frontname/index/action',
    .......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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