2

I'm trying to add in a single file on the root folder something than given a sku or id (it can be either get or post)

it will return me the stock as plain text.

I'm trying to figure out if there is a way to do something like.

$products=Mage::getModel('catalog/product')->getCollection();
foreach($products as $_product){
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();
}

but this file isn't connected to Magento

sorry for the bad english,

1
  • Are you looking something for like this?
    – Kul
    Commented Nov 9, 2016 at 21:45

2 Answers 2

2

Copy the index.php, and instead of calling Mage::run(), call:

Mage::app('admin', 'store');

Then after the call above, you can run your Magento code with full access

BIG WARNING NOTE: This grants unauthenticated access to whatever your script does.

1
  • thanks, I know it-s not the best aproach, but we need an endpoint to be accesed by anyone to show stock based on sku
    – Saikios
    Commented Nov 9, 2016 at 22:01
1

This is the proper way to call the magento functionality in your custom file on root of magento. You have to call these first 2 line in your code and rest coding as per magento standards.

<?php
require_once 'app/Mage.php';
Mage::app();

$products=Mage::getModel('catalog/product')->getCollection();
foreach($products as $_product){
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
    echo $stock->getQty();
    echo $stock->getMinQty();
    echo $stock->getMinSaleQty();
}
?>

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.