Add Custom Attribute to Category to Select a CMS Page
-
16-10-2019 - |
Question
I'd like to be able to choose a CMS page with a custom attribute on a category. I know how to add a custom attribute to a category and have it be a select box, but is it possible to have the select populated with a list of CMS pages?
Solution
I would have preferred to have asked a comment but I don't have the reputation for that. However, to get you started, here is what you might want to try:
in your installer you can add in an attribute, something like this:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_category", "cmspage", array(
"type" => "int",
"backend" => "",
"frontend" => "",
"label" => "CMS Page",
"input" => "select",
"class" => "",
"source" => "mymodule/eav_entity_attribute_source_cmspage",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
"visible" => true,
"required" => false,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,
"visible_on_front" => false,
"unique" => false,
"note" => "CMS Page"
));
$installer->endSetup();
Then to get your cms pages to show you can then do something like this:
<?php
class Mycompany_Mymodule_Model_Eav_Entity_Attribute_Source_Cmspage extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
/**
* Retrieve all options array
*
* @return array
*/
public function getAllOptions()
{ $cmspages=Mage::getModel('cms/page')->getCollection();
$pages=array();
foreach($cmspages as $scmspage) {
$pages[$cmspage->getId()]=$cmspage->getTitle();
}
array_unshift($pages, array('label' => 'None', 'value' => ''));
return($pages);
}
/**
* Retrieve option array
*
* @return array
*/
public function getOptionArray()
{
$_options = array();
foreach ($this->getAllOptions() as $option) {
$_options[$option["value"]] = $option["label"];
}
return $_options;
}
public function getOptionText($value)
{
$options = $this->getAllOptions();
foreach ($options as $option) {
if ($option["value"] == $value) {
return $option["label"];
}
}
return false;
}
Note this is not tested, however, I hope it gets you along the way.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange