Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Created November 22, 2013 09:17
Show Gist options
  • Save maxpoletaev/7597125 to your computer and use it in GitHub Desktop.
Save maxpoletaev/7597125 to your computer and use it in GitHub Desktop.
Snippet for MiniShop2. Get vendors from category.
<?php
$pdo = $modx->getService('pdoTools');
$fieldName = $modx->getOption('fieldName', $scriptProperties, 'vendor');
$fieldClass = $modx->getOption('fieldClass', $scriptProperties, '');
$firstOption = $modx->getOption('firstOption', $scriptProperties, '');
$parent = $modx->getOption('parent', $scriptProperties, 0);
$depth = $modx->getOption('depth', $scriptProperties, 10);
$limit = $modx->getOption('limit', $scriptProperties, 100);
$tplOuter = $modx->getOption('tplOuter', $scriptProperties, '@INLINE <select name="[[+name]]" class="[[+class]]">[[+rows]]</select>');
$tplRow = $modx->getOption('tplRow', $scriptProperties, '@INLINE <option value="[[+id]]">[[+name]]</option>');
$q = $modx->newQuery('msProduct');
$q->innerJoin('msProductData', 'Data', 'msProduct.id = Data.id');
$q->innerJoin('msVendor', 'Vendor', 'Data.vendor = Vendor.id');
$q->leftJoin('msCategoryMember', 'Member', 'Member.product_id = msProduct.id');
$q->select('Vendor.id, Vendor.name');
$q->groupby('Vendor.id');
$q->sortby('Vendor.name');
$q->limit($limit);
$parents = $modx->getChildIds($parent, $depth);
$parents[] = $parent;
$q->where(array('msProduct.parent:IN' => $parents));
$q->orCondition(array('Member.category_id:IN' => $parents));
$options = '';
if (!empty($firstOption)) {
$options .= $pdo->getChunk($tplRow, array(
'id' => '',
'name' => $firstOption
));
}
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$options .= $pdo->getChunk($tplRow, $row);
}
}
$output = $pdo->getChunk($tplOuter, array(
'name' => $fieldName,
'class' => $fieldClass,
'rows' => $options,
));
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment