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,

有帮助吗?

解决方案

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.

其他提示

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();
}
?>
许可以下: CC-BY-SA归因
scroll top