cancel
Showing results for 
Search instead for 
Did you mean: 

How to Assign Custom Product Type to Group Product?

How to Assign Custom Product Type to Group Product?

image.pngHi 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?

2 REPLIES 2

Re: How to Assign Custom Product Type to Group Product?

 

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:

Steps to Assign Custom Product Type Products to a Group Product:

  1. Ensure Your Custom Product Type Supports Group Products:

    • Make sure your custom product type extends from \Magento\GroupedProduct\Model\Product\Type\Grouped or has the necessary implementation to be part of a grouped product.
  2. Create a Script to Assign Products:

    • Use a custom script to programmatically add your custom product type products to the group product. Below is a sample script to achieve this.

Create a PHP script in your Magento root directory and run it from the command line or through your web server.

 

php
 
<?php require 'app/bootstrap.php'; use Magento\Framework\App\Bootstrap; use Magento\Framework\App\ObjectManager; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); $productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository'); $groupedProductFactory = $objectManager->get('Magento\GroupedProduct\Model\Product\Type\GroupedFactory'); $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface'); $productType = 'new_product_type'; // Your custom product type $groupProductSku = 'group-product-sku'; // SKU of the group product $customProductSkus = ['custom-product-sku1', 'custom-product-sku2', 'custom-product-sku3']; // SKUs of custom products try { // Load the group product $groupProduct = $productRepository->get($groupProductSku); // Check if the product is of type grouped if ($groupProduct->getTypeId() !== 'grouped') { throw new \Exception("The specified product is not a grouped product."); } // Prepare custom products $customProducts = []; foreach ($customProductSkus as $sku) { $customProduct = $productRepository->get($sku); if ($customProduct->getTypeId() !== $productType) { throw new \Exception("Product {$sku} is not of the custom product type."); } $customProducts[$customProduct->getId()] = [ 'qty' => 1, // Quantity for the grouped product 'price' => $customProduct->getPrice() ]; } // Add products to the grouped product $groupedProductFactory->create()->setProduct($groupProduct)->setAssociatedProducts($customProducts); $productRepository->save($groupProduct); echo "Products have been successfully assigned to the group product."; } catch (\Exception $e) { echo "Error: " . $e->getMessage(); }

Notes:

  • Ensure Backups: Always back up your database before running custom scripts.
  • Adjust Script as Needed: Modify the SKU values and custom product type as required.

This script assumes basic familiarity with Magento’s object manager and product repository. If you're unsure, consider consulting with a Magento developer.

Re: How to Assign Custom Product Type to Group Product?

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.