CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to use Magento's product type system for custom product types

Step 1: Create a new module

Create a new directory in app/code/local and add the necessary files for your module. For example, let's create a module called MyCompany_CustomProductType.

Step 2: Define the module's configuration

In app/code/local/MyCompany/CustomProductType/etc/config.xml, add the following code:


		        <?xml version="1.0"?>
        <config>
            <modules>
                <MyCompany_CustomProductType>
                    <version>1.0.0</version>
                </MyCompany_CustomProductType>
            </modules>
            <global>
                <blocks>
                    <catalog>
                        <rewrite>
                            <product_type>MyCompany_CustomProductType_Block_Product_Type</product_type>
                        </rewrite>
                    </catalog>
                </blocks>
            </global>
        </config>
	

Step 3: Create the custom product type block

In app/code/local/MyCompany/CustomProductType/Block/Product/Type.php, add the following code:


	class MyCompany_CustomProductType_Block_Product_Type extends Mage_Catalog_Block_Product_Type_Abstract
    {
    public function __construct()
    {
        parent::__construct();
        $this->setType('mycompany_customproducttype');
    }
    }
	

Step 4: Define the custom product type

In app/code/local/MyCompany/CustomProductType/etc/product_type.xml, add the following code:


	<?xml version="1.0"?>
	<config>
    <sections>
        <producttypes>
            <groups>
                <mycompany_customproducttype translate="label">
                    <label>My Company Custom Product Type</label>
                    <fields>
                        <!-- Add your custom fields here -->
                        <my_field translate="label">
                            <label>My Custom Field</label>
                            <frontend_type>text</frontend_type>
                            <required>0</required>
                        </my_field>
                    </fields>
                </mycompany_customproducttype>
            </groups>
        </producttypes>
    </sections>
	</config>
	

Step 5: Register the custom product type

In app/code/local/MyCompany/CustomProductType/etc/config.xml, add the following code:


	<?xml version="1.0"?>
	<config>
		<!-- ... -->
		<global>
			<!-- ... -->
			<catalog_product_type_config>
			<mycompany_customproducttype translate="label">
			<label>My Company Custom Product Type</label>
			<options_model>mycompany_customproducttype/product_type_option</options_model>
				</mycompany_customproducttype>
			</catalog_product_type_config>
		</global>
	</config>
	

Step 6: Create the options model

In app/code/local/MyCompany/CustomProductType/Model/Product/Type/Option.php, add the following code:


	class MyCompany_CustomProductType_Model_Product_Type_Option extends Mage_Catalog_Model_Product_Type_Option_Abstract
    {
    public function getOptionArray()
    {
        // Return an array of options for your custom product type
        return array(
            'option1' => 'Option 1',
            'option2' => 'Option 2',
        );
    }
    }
	

Step 7: Add the custom product type to the product grid

In app/code/local/MyCompany/CustomProductType/etc/grid.xml, add the following code:


	
	
    

        
        
            

            
            

                
                
                    

                    
                    mycompany_customproducttype/column/renderer

                

            

        

    

	
	

Step 8: Implement the custom column renderer

In app/code/local/MyCompany/CustomProductType/etc/column/renderer.php, add the following code:


	class MyCompany_CustomProductType_Column_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
    public function render(Varien_Object $row)
    {
        // Render your custom column content here
        return 'Hello, World!';
    }
    }
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.