cancel
Showing results for 
Search instead for 
Did you mean: 

Help please creating a custom product type in Magento 2

SOLVED

Help please creating a custom product type in Magento 2

Hi,

 

I'm trying to create a custom product but with no luck at all. 

 

I've followed the examples on a couple of sites (http://magenest.com/creating-new-product-type-and-product-attribute-in-magento-2/ and http://magento.stackexchange.com/questions/51914/how-to-add-a-new-product-type-in-magento-2-magestac...

 

I've create the product_types.xml file and a php file for my model.

 

I may be doing something totally wrong or very stupid (we've all done it), or maybe the format has changed since the final version of 2 came out?

 

I can't find anything in the official docs (again, maybe I'm missing it), and only a handful of sites talking about it.

 

I'm a very experienced programmer but new to Magento 2.

 

I'm running in developer mode but can't see any errors anywhere.

 

The custom product is not showing when I go to Add Product.

 

Any ideas please? I've spent all day on this so far!

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Help please creating a custom product type in Magento 2

Absolutely you need a module.

app/etc is not the place to put your custom configuration files. Also, copying and pasting my code examples will not just work either. [Vendor] needs replacing with your own Vendor namespace.

 

See Github for a full example.

Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner

View solution in original post

11 REPLIES 11

Re: Help please creating a custom product type in Magento 2

Can you show your product_types.xml and your class please?
Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner

Re: Help please creating a custom product type in Magento 2

Certainly. Thanks for offering to hep.

 

this file is stored in /etc/product_types.xml

 

 

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Catalog/etc/product_types.xsd">
    <type name="myproduct" label="My Product" modelInstance="Vendor\MyCo\Model\Product\Type\MyProduct" composite="false" isQty="true" canUseQtyDecimals="false" sortOrder="70">
        <customAttributes>
            <attribute name="refundable" value="false"/>
            <attribute name="is_real_product" value="true"/>
        </customAttributes>
    </type>
</config>

 

this file is stored in /vendor/myco/model/product/type/MyProduct.php

 

 

<?php

namespace Vendor\MyCo\Model\Product\Type;

class MyProduct extends \Magento\Catalog\Model\Product\Type\AbstractType {
    public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
    {
    }
}

I also have an empty directory called /vendor/myco/model/product/type/MyProduct

 

Thanks

 

Re: Help please creating a custom product type in Magento 2

You have actually caught me in the middle of writing a small tutorial on custom product type creation and from what you have posted your code looks sound.

 

Couple of things i would do.

 

1. Check your module shows up in stores > configuration > advanced > advanced.

2. Clear your cache `bin/magento cache:flush`

Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner

Re: Help please creating a custom product type in Magento 2

Thanks. I thought it seemed right but... One of those where I was banging my head all day yesterday - and still am today...

 

No, it's not showing in that list.

 

I've flushed the cache and checked again, still not showing in the list or in the Add Product drop down... Smiley Sad

 

I've checked ownership of all files and they are owned by magento, group www-data (sames as all other magento files)

 

I've just set the file Permissions to 660, folders to 770. Still not working.

 

Anything else I may be missing?

 

Thank you

 

Re: Help please creating a custom product type in Magento 2

It sounds to me like it is an issue with your module then and not the code to create a new product type.

 

You should try running `bin/magento module:status` to see if your module shows up in that list. My guess is it will **not** show in the "List of enabled modules" but it might show in the "List of disabled modules". If that is the case then run `bin/magento module:enable [Vendor]_[ModuleName] followed by `bin/magento setup:upgrade`

 

My Module Basics tutorial might be of some help to make sure your module is correctly registered.

 

Regards,

Andy

Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner

Re: Help please creating a custom product type in Magento 2

Thanks so much for helping but I'm still not seeing any results. Running bin/magento module:status shows nothing in my vendor name.

 

I had a look at your page on modules but I'm not trying to create a module (yet), just a product type.

 

I saw the link on your page to http://www.smartiehastheanswer.co.uk/magento2/how-to-create-a-new-product-type-in-magento2.html and read that

 

This suggests putting the product_types.xml in /app/code/myvendorname/AffiliateProductType/etc/product_types.xml - but I've been putting that file in /app/etc/

 

Does it matter?

 

I renamed the one in /app/etc/ to .old and put the following code from your site into /app/code/myvendorname/AffiliateProductType/etc/product_types.xml

 

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
    <type name="generic" label="Generic Product" modelInstance="[Vendor]\GenericProductType\Model\Product\Type\Generic" indexPriority="100" sortOrder="10">
        <customAttributes>
            <attribute name="refundable" value="true"/>
        </customAttributes>
    </type>
    <composableTypes>
        <type name="generic" />
    </composableTypes>
</config>

 

According to your page, I should now see Generic Product from the drop down on Add Product (albeit with an error), but I'm not. I also tried it renaming [Vendor] in modelInstance to myvendorname

 

I know I must be doing something REALLY stupid here, but I have no idea what!

 

Any ideas?

 

Thank you

 

Re: Help please creating a custom product type in Magento 2

Is it the case I NEED to create a custom model to have a custom product type?! ;0

Re: Help please creating a custom product type in Magento 2

Absolutely you need a module.

app/etc is not the place to put your custom configuration files. Also, copying and pasting my code examples will not just work either. [Vendor] needs replacing with your own Vendor namespace.

 

See Github for a full example.

Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner

Re: Help please creating a custom product type in Magento 2

ah thanks, nothing I've read so far has suggested this, so I presumed there was no direct connection between a new product type and a new module. I'll give it a try and let you know!