Skip to content

Instantly share code, notes, and snippets.

@bigin
Last active December 27, 2018 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigin/0d4805b0933d697cc02d481223e37f5d to your computer and use it in GitHub Desktop.
Save bigin/0d4805b0933d697cc02d481223e37f5d to your computer and use it in GitHub Desktop.
<?php
// Enter new field name
$fieldname = 'group';
// Get the SC processor instance
$processor = $catalog->processor;
// Get the IM instance
$imanager = $catalog->imanager;
// Get ItemMapper instance
$itemMapper = $imanager->getItemMapper();
// Get the dummy category
$dummy = $imanager->getCategoryMapper()->getCategory($processor->config->dummyCategoryId);
// If the dummy isn't null
if($dummy) {
// Get the FieldMapper instance and initialize the fields of the current category
$fiedMapper = new FieldMapper();
$fiedMapper->init($dummy->id);
if(!$fiedMapper->fieldNameExists($fieldname)) {
// Enter new field data you want to add to your categories
$customField = new Field($dummy->id);
$customField->name = $fieldname;
$customField->label = 'My Group by field';
$customField->type = 'text';
$customField->position = null;
$customField->default = '';
$customField->info = 'Put the group by value here (e.g. Group 1, or Group 2, etc.)';
$customField->required = null;
$customField->minimum = null;
$customField->maximum = null;
// Save the field
if($customField->save()) {
// Overwrite items, so that new 'group by' field can be used
$itemMapper->init($dummy->id);
if($itemMapper->items) {
foreach($itemMapper->items as $item) {
$item->save();
// Save SimpleItem object
$itemMapper->alloc($dummy->id);
$itemMapper->simplify($item);
$itemMapper->save();
}
echo 'The category '.$dummy->name.' got a new '.$customField->name.' field.<br>';
}
}
} else {
echo 'The '.$dummy->name.' category already contains the '.$fieldname.' field.<br>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment