So I know this is a slightly old thread... but for anyone wanting stock management - at a VERY basic level - I made a really simple TV + snippet to control it for a small club shop.
NOTE - the problem with calling the snippet at this stage is should the user keep revisiting the same page/refreshing it, the stock will be deducted each time.
Anyway, for for it's worth:
TV name "stock_count" (my ID 29) - assign to shop product template(s)
Snippet:
[[!update_stock? &product=`[[+id]]` &qty=`[[+count]]`]]
<?php
//set vars
$tvId = 29;
$tvName = "stock_count";
$stockCount = 0;
$resourceId = (int) $modx->getOption('product', $scriptProperties);
$qtyOrdered = (int) $modx->getOption('qty', $scriptProperties, 1);
if(!empty($resourceId))
{
//Get Absolute Current Stock (bypass cache)
$tvr = $modx->getObject('modTemplateVarResource', array(
'tmplvarid' => $tvId,
'contentid' => $resourceId
));
if ($tvr) {
$stockCount = $tvr->get('value');
}else {
$tv = $modx->getObject('modTemplateVar', $tvId);
if ($tv) $stockCount = $tv->get('default_text');
}
//Set New Stock Value
$stockCount = $stockCount - $qtyOrdered;
$page = $modx->getObject('modResource', $resourceId);
if (!$page->setTVValue($tvName, $stockCount)) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your TV...');
}
}else{
$modx->log(xPDO::LOG_LEVEL_ERROR, 'No resource ID');
}
I call the snippet in a custom &tplRow on the "successful order" page:
[[!msGetOrder? &tplRow=`tpl.msCart.SuccessfulOrder.row`]]
You can display the stock on the product page calling the TV uncached:
Might help somebody... or maybe someone can tell me where's a better place to call the snippet.
Cheers
Kyle