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:


	
	
    
        
            1.0.0
        
    
    
        
            
                
                    MyCompany_CustomProductType_Block_Product_Type
                
            
        
    
	
	

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:


	
	
    
        
            
                
                    
                    
                        
                        
                            
                            text
                            0
                        
                    
                
            
        
    
	
	

Step 5: Register the custom product type

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


	
	
    
    
        
        
            
                
                mycompany_customproducttype/product_type_option
            
        
    
	
	

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.