Last active
December 27, 2018 20:05
-
-
Save bigin/0d4805b0933d697cc02d481223e37f5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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