3

(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.

0

1 Answer 1

2

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

5
  • 1
    Goodness gracious, I even used require / include on another website (non-CMS), so as I said, "tupid mistake" - altering the path a bit using the brackets made the page not throw the stated error (doing the rest now). Thanks alot, guess I would have searched years for the answer as I didn't expect THIS to be the problem ;) (Also surprised by how fast the answer came. Accepting the answer :) )
    – jekkst
    Aug 19, 2016 at 10:54
  • 1
    Glad i could help, always helps to have a fresh pair of eyes :)
    – DanCarlyon
    Aug 19, 2016 at 10:58
  • 2
    Weird, because require 'file.php' is valid syntax and actually the correct way, the brackets do nothing. require and require_once are not functions! I bet the problem really was the path itself Aug 19, 2016 at 11:31
  • 1
    true, i've always done it with brackets myself, maybe a quirk in this specific version of php ¯_(ツ)_/¯
    – DanCarlyon
    Aug 19, 2016 at 12:02
  • 2
    Weird, trying it with works, while not using them gives the error. Just checked, and the website I mentioned actually DOESN'T have brackets, and works. Well, I for myself don't need to know the specific reason for that, as long as I know I should try both versions :P
    – jekkst
    Aug 19, 2016 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.