Question

(Edit: The first line went missing, just the greetings) Hello guys! I'm both new to Stack Exchange and relatively new to Magento.

What I want to do:

Use a self-written JQuery Script to get the ID of a currently selected Simple Product (which is the child of a Configurable Product), call a PHP-File to load the stock-quantity of this product and read it into JQuery again.

However, when trying to call the PHP-File, I get a Internal server error - when trying to open the file directly, I get the HTTP Error 500 (both should be the same as far as I know).

What I have

My current JQuery (the part using Ajax) (update.js):

function getQuantity(productId){
    if (productId === null)
    {
        Qty = 0;    
    }
    else
    {
        var simpleIdJSON = '[{"simpleIdJSON":"' + productId + '"}]';
        $j.ajax({
            url: '/js/stock/qty.php',
            type: "POST",
            dataType: 'json',
            data: simpleIdJSON,
            success: function(data){
                alert(data)
            },
            failure: function(msg){
                alert(msg)
            },
            error: function(xhr, status, error) {
                alert(error);
            }
        }); 
    }
}

My current PHP (qty.php):

<?php
    //load and build up Mage
    require_once 'app/Mage.php';
    umask(0);
    Mage::app('default');
    Mage::setIsDeveloperMode(true);
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    //get the data of the chosen product
    $model = Mage::getModel('catalog/product'); 
    $productId = $_POST['simpleIdJSON'];
    $_product = $model->load($productId);

    //get the quantity of the chosen product and return it 
    $qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
    echo json_encode($qty);
?>

I don't receive any Error when I delete every content from qty.php, while the errors are already comming up when using both require_once 'app/Mage.php' or require 'app/Mage.php' and also tried altering the path using /app.., ../app.., /../app.. and ../../app... The path to the JQuery and PHP files is: ROOT/js/stock/. I do not think it has anything to do with the Path itself, but rather the way I try to get the Classes. If anyone could tell me what (possibly stupid) mistake I'm making, I would be very happy. Criticism, especially about this post (which I would then edit), are welcomed.

Was it helpful?

Solution

Looking at your qty.php you require function is wrong and should be

<?php
//load and build up Mage
require_once('app/Mage.php');

Require reference: http://php.net/manual/en/function.require-once.php

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top