Hi everyone,
I have created a custom product type called new_product_type and have successfully created 3 products of this type. Now, I would like to assign these products to a group product. However, I'm not sure how to achieve this.
Could anyone provide guidance or share any relevant code snippets to help me assign my custom product type products to a group product in Magento?
To assign your custom product type products to a group product in Magento, you need to follow a few steps. Unfortunately, Magento does not directly support assigning custom product types to group products through the standard admin interface, so you’ll need to use custom code for this. Here’s a general approach:
Ensure Your Custom Product Type Supports Group Products:
Create a Script to Assign Products:
Create a PHP script in your Magento root directory and run it from the command line or through your web server.
This script assumes basic familiarity with Magento’s object manager and product repository. If you're unsure, consider consulting with a Magento developer.
Hello @henilmarel6b07,
In Magento 2, Grouped Products allows you to group multiple products (simple products, for example) into a single product that can be purchased together or separately. To assign your custom product type (new_product_type) to a grouped product, you'll need to ensure that your custom product type is compatible with grouped products
Here is the basic example of how to create new product and that type of product is slowed to add it on grouped product
Step – 1:
You first you need to declare your new product type using the product_types.xml located in the etc folder of your module
In app/code/[Vendor]/Module/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="Milople\NewType\Model\Product\Type\Generic" indexPriority="100" sortOrder="10"> <customAttributes> <attribute name="refundable" value="true"/> </customAttributes> </type> <composableTypes> <type name="generic" /> </composableTypes> </config>
Step – 2 :
Create model file at app/code/[Vendor]/Module/Model/Product/Type/Generic.php
<?php namespace Milople\NewType\Model\Product\Type; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product\Type\AbstractType; class Generic extends AbstractType { const TYPE_CODE = 'generic'; public function deleteTypeSpecificData(Product $product) { } }
Step – 3 :
Price attributes, among others, are set to apply to certain product types. We need to tell Magento that prices should also apply to our generic product type.
We do this in a setup script.
In app/code/[Vendor]/Module/Setup/InstallData.php
<?php namespace Milople\NewType\Setup; use Magento\Catalog\Model\Product; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Milople\NewType\Model\Product\Type\Generic; class InstallData implements InstallDataInterface { protected $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var \Magento\Eav\Setup\EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); // price attributes we want to apply // to our product type $attributes = [ 'price', 'special_price', 'special_from_date', 'special_to_date', 'minimal_price', 'tax_class_id' ]; foreach ($attributes as $attributeCode) { $relatedProductTypes = explode( ',', $eavSetup->getAttribute(Product::ENTITY, $attributeCode, 'apply_to') ); if (!in_array(Generic::TYPE_CODE, $relatedProductTypes)) { $relatedProductTypes[] = Generic::TYPE_CODE; $eavSetup->updateAttribute( Product::ENTITY, $attributeCode, 'apply_to', join(',', $relatedProductTypes) ); } } } }
By ensuring that your custom product type is groupable and following the above steps (manually or programmatically), you can assign your custom product type products to a grouped product in Magento 2
If the issue will be resolved, Click Kudos & Accept as a Solution.