Skip to content

Instantly share code, notes, and snippets.

@vgrish
Created March 26, 2017 07:32
Show Gist options
  • Save vgrish/9f5377eef8d7c72f87a4b625f3e1729a to your computer and use it in GitHub Desktop.
Save vgrish/9f5377eef8d7c72f87a4b625f3e1729a to your computer and use it in GitHub Desktop.
<?php
if ($modx->event->name != 'msopOnModificationSave') {
return;
}
$modification = $modx->getOption('modification', $scriptProperties);
if (!$modification) {
return;
}
/** @var msProduct $product */
$product = $modification->getOne('Product');
if (!$product) {
return;
}
$sklad = trim($product->getTVValue('sklad'));
if (!in_array($sklad, array('', 'магазин'))) {
return;
}
$count = 0;
$q = $modx->newQuery('msopModification');
$q->where(array(
'rid' => $product->get('id'),
'type' => 1,
'active' => 1,
'count:>' => 0,
));
$q->select('SUM(count)');
if ($stmt = $q->prepare()) {
$count = (int)$modx->getValue($stmt);
}
if (empty($count)) {
$product->setTVValue('ostatki', $count);
$product->setTVValue('sklad', 'нет в наличии');
} else {
$product->setTVValue('ostatki', $count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment