I want to programmatically update product information, such as quantity, price, etc. (from outside of the Magento source directory.)
How can I do that?
Magento is pretty easy to bootstrap. If you want a standalone script where you can access all your functions, just add the following at the top of your PHP file :
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/../app/Mage.php'; //or whatever location your Mage.php file is
Mage::app(Mage_Core_Model_Store::ADMIN_CODE); //initialization with another store is possible
After that you can load all your models. To update your products I suggest you two ways. The regular one :
Mage::getModel('catalog/product')->setStoreId($myStoreId)->load($myProductId)
->setPrice(50)
->save();
Or the API model usage :
$api = Mage::getModel('catalog/product_api_v2');
$api->update($productId, $productData, $store, $identifierType);
I'd highly recommend leveraging the REST APIs available in M2x to Create/Update products and its attributes.
Note: You have the option of using OAuth or Bearer Tokens in Magento 2 to Authenticate/Authorize your API invocations.
You can find additional information on all the APIs available in Magento 2.1 here - http://devdocs.magento.com/swagger/index_21.html
You'll find specifics of the APIs you need under the grouping titled catalogProductRepositoryV1
GET /V1/products
GET /V1/products/{sku}
POST /V1/products
PUT /V1/products/{sku}
I haven't tested the code, but I think something like this should do the trick:
$BEARER_TOKEN_TO_USE_FOR_TRANSACTION = 'XYZ';
$REQUEST_HEADER = array(
"Authorization => Bearer ". $BEARER_TOKEN_TO_USE_FOR_TRANSACTION ,
"cache-control: no-cache",
"content-type: application/json"
);
$REQUEST_URL='INSTANCE_URL/rest/V1/products';
$PRODUCT_DATA_TO_USE ='{
"product": {
ENTER_PRODUCT_ATTRIBUTES_AS_JSON
} }';
$CURL_OBJ = curl_init($REQUEST_URL);
$CURL_OPTIONS_ARRAY_TO_USE = array (
CURLOPT_HTTPHEADER => $REQUEST_HEADER,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $PRODUCT_DATA_TO_USE,
CURLOPT_URL => $REQUEST_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
);
curl_setopt_array($CURL_OBJ, $CURL_OPTIONS_ARRAY_TO_USE);
$result = curl_exec($CURL_OBJ);
$result = json_decode($result);
echo 'Output -> " . $result;