Created
December 27, 2018 14:10
-
-
Save bigin/c7634296a469eb297c4ffd0535a4afc5 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 = 'my_custom_field'; | |
// Get the SC processor instance | |
$processor = $catalog->processor; | |
// Get the IM instance | |
$imanager = $catalog->imanager; | |
// Set the number of categories to be selected to 1000 or more | |
$processor->config->frontendCatPerPage = 1000; | |
// Get the SC categories and set 'section' parameter to 'frontend' | |
$catData = $processor->getCategories( | |
['section' => 'frontend'] | |
); | |
// If the catData isn't null | |
if(isset($catData['total']) && $catData['total'] > 0) { | |
foreach($catData['categories'] as $category) { | |
// Get the FieldMapper instance and initialize the fields of the current category | |
$fiedMapper = new FieldMapper(); | |
$fiedMapper->init($category->id); | |
// Check if the field already exists. | |
if(!$fiedMapper->fieldNameExists($fieldname)) { | |
// Enter new field data you want to add to your categories | |
$customField = new Field($category->id); | |
$customField->name = $fieldname; | |
$customField->label = 'My Custom Field'; | |
$customField->type = 'text'; | |
$customField->position = null; | |
$customField->default = ''; | |
$customField->info = 'Put the field info here'; | |
$customField->required = null; | |
$customField->minimum = null; | |
$customField->maximum = null; | |
// Save the field | |
if($customField->save()) { | |
echo 'The category '.$category->name.' got a new '.$customField->name.' field.<br>'; | |
} | |
} else { | |
echo 'The '.$category->name.' category already contains the '.$fieldname.' field.<br>'; | |
} | |
} | |
} else { | |
echo 'No categories found.<br>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment