Creating Custom Product Types with Custom Attributes in Magento
Creating custom product types in Magento is a powerful way to extend basic functionality and meet specific business requirements. In this guide, we’ll walk through the process of creating a new product type with custom attributes step-by-step.
Understanding the Current State of References
You’ve already explored two popular references:
-
Paul Clegg’s Tutorial This tutorial provides a basic overview of creating a custom product type but lacks detailed implementation for attributes.
-
Magestore’s Guide While this guide offers more comprehensive steps, it might not cover your specific requirements or the latest Magento versions.
Both resources provide valuable insights but fall short in offering a complete solution for adding custom attributes to your new product type. Let’s dive into a detailed approach.
Step 1: Prepare Your Environment
Before we begin, ensure you have:
- A local Magento installation or access to the server.
- Familiarity with basic PHP and XML editing.
- A backup of your current site in case something goes wrong.
Step 2: Define Your Custom Product Type
First, let’s create a new folder structure under app/code/local following the standard Magento module naming convention:
app/
code/
local/
Magicento/
etc/
config.xml
Model/
Mage/
Magicento/
Model/
Magicento.php
Step 3: Implementing the Custom Product Type
In your config.xml file, include the following configuration to define your new product type:
<config>
<modules>
<Magicento_Magicento>
<version>0.1.0</version>
</Magicento_Magicento>
</modules>
<global>
<models>
<magicento>
<class>Magicento_Model</class>
<resourceModel>magicento_resource</resourceModel>
</magicento>
</models>
<resources>
<magicento_setup>
<setup>
<module>Magicento_Magicento</module>
</setup>
</magicento_setup>
</resources>
</global>
<adminhtml>
<!-- Add your custom admin configuration here -->
</adminhtml>
</config>
Next, create the Magicento.php file under app/code/local/Magicento/Model/Magicento/ to implement the core functionality of your product type:
<?php
class Magicento_Model_Magicento extends Mage_Core_Model_Abstract {
public function _construct() {
parent::_construct();
$this->_init('magicento/magicento');
}
}
Step 4: Creating Custom Attributes
To create custom attributes for your new product type, navigate to app/code/local/Magicento/sql/magicento_setup/install-0.1.0.sql and add the following SQL:
CREATE TABLE magicento_attributestable (
attribute_id int(11) unsigned NOT NULL auto_increment,
entity_type_id int(11),
attribute_code varchar(255) NOT NULL default '',
backend_model varchar(255),
frontend_model varchar(255),
frontend_input varchar(255),
frontend_label varchar(255),
frontend_class varchar(255),
source_model varchar(255),
is_global int(1) NOT NULL default '0',
is_searchable int(1) NOT NULL default '0',
is_filterable int(1) NOT NULL default '0',
is_comparable int(1) NOT NULL default '0',
is_visible_in_advanced_search int(1) NOT NULL default '0',
is_html_allowed_on_front int(1) NOT NULL default '0',
used_in_product_listing int(1) NOT NULL default '0',
used_for_sort_by int(1) NOT NULL default '0',
is_filterable_in_search int(1) NOT NULL default '0',
apply_to int(1) NOT NULL default '0',
is_used_for_price rules int(1) NOT NULL default '0',
position int(11),
is_wysiwyg_enabled int(1) NOT NULL default '0',
PRIMARY KEY (attribute_id)
) ENGINE=InnoDB;
Step 5: Extending the Product Type
To use your custom product type in the Magento admin, you’ll need to extend the core Mage_Catalog_Model_Product_Type_Abstract class. Create a new file at app/code/local/Magicento/Model/Product/Type/Abstract.php:
<?php
class Magicento_Model_Product_Type_Abstract extends Mage_Catalog_Model_Product_Type_Abstract {
// Add your custom product type functionality here
}
Step 6: Activating Your Custom Product Type
To make your new product type available in the Magento admin, add an entry to app/code/core/Mage/Catalog/etc/config.xml:
<config>
<!-- ... other config code -->
<catalog_product_type>
<types>
<magicento translate="label">
<label>Magicento Product</label>
</magicento>
</types>
</catalog_product_type>
</config>
Conclusion
Creating a custom product type with custom attributes in Magento is a complex task that requires a solid understanding of the core platform. By following this step-by-step guide, you’ll be able to extend your Magento installation’s capabilities and meet specific business requirements.
Remember, always test your changes thoroughly before deploying them to a production environment. If you’re looking for professional help or need further guidance on Magento development, feel free to explore our comprehensive Magento services at LaravelCompany.
Happy coding!