CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to create custom Magento product types

Step 1: Create a new module

Create a new directory in app/code/local with your company name and module name, for example MyCompany/MyCustomProductType.

Step 2: Create the module configuration file

Create a file named config.xml in the module directory with the following content:


    <?xml version="1.0"?>
    <config>
        <modules>
            <MyCompany_MyCustomProductType>
                <active>true</active>
                <codePool>local</codePool>
            </MyCompany_MyCustomProductType>
        </modules>
    </config>
	

Step 3: Create the product type class

Create a new file named Product/Type/MyCustomProductType.php in the module directory with the following content:


	class MyCompany_MyCustomProductType_Model_Product_Type_MyCustomProductType extends Mage_Catalog_Model_Product_Type_Abstract
  {
    protected function _construct()
    {
        parent::_construct();
        $this->_label = 'My Custom Product Type';
    }

    public function getCode()
    {
        return 'mycustomproducttype';
    }
  }
	

Step 4: Create the product type model

Create a new file named Product/Model/Product/Mycustomproducttype.php in the module directory with the following content:


	class MyCompany_MyCustomProductType_Model_Product_MyCustomProductType extends Mage_Catalog_Model_Product_Abstract
  {
    public function getAvailabilityMessage($isInStock = true)
    {
        return 'This is a custom availability message';
    }
  }
	

Step 5: Register the product type

In your module's etc/config.xml file, add the following code:


	<config>
    <!-- ... -->
    <global>
        <models>
            <product>
                <type>
                    <mycustomproducttype>MyCompany_MyCustomProductType_Model_Product_MyCustomProductType</mycustomproducttype>
                </type>
            </product>
        </models>
    </global>
	</config>
	

Step 6: Add the product type to the product form

In your module's etc/config.xml file, add the following code:


	<config>
    <!-- ... -->
    <frontend>
       <module_layout_update>
        <mycustomproducttype_before_product_form_product_info mycustomproducttype_before_product_form_product_info />
        </module_layout_update>
        <layout_update>
          <mycustomproducttype_before_product_form_product_info>
             <reference name="product.info">
             <block template="mycompany/mycustomproducttype/form.phtml" name="mycustomproducttype_before_product_form_product_info" as="mycustomproducttype" />
             </reference>
          </mycustomproducttype_before_product_form_product_info>
        </layout_update>
    </frontend>
	</config>
	

Step 7: Create the template

Create a new file named form.phtml in your module's view/frontend/templates/mycompany/mycustomproducttype directory with the following content:


		
This is a custom form field for my custom product type

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.