How to Allow Individual Group Product Selection for Separate Cart Addition - magento source24


How to Allow Individual Group Product Selection for Separate Cart Addition:- 

Hello magento friends in this blog post i'm share with you to How to Allow Individual Group Product Selection for Separate Cart Addition. In Magento 2, by default, group products are intended to be added to the cart as a single unit representing the group. However, if you want to allow customers to add individual simple products from a group product to the cart separately, you'll need to implement a customization. This involves creating a module and adding some custom code. Here's a high-level guide on how to achieve this:

Please Note: Customizing Magento requires a good understanding of the platform's architecture and coding practices. Make sure to test this on a development environment before applying it to a live store.

Step 1): Create a Custom Module:-

  • Create a new directory for your module in app/code directory. For example, app/code/YourNamespace/YourModule.
  • Create the necessary module registration file at app/code/YourNamespace/YourModule/registration.php:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'YourNamespace_YourModule',

    __DIR__

);

  • Create the module declaration file at app/code/YourNamespace/YourModule/etc/module.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="YourNamespace_YourModule" setup_version="1.0.0"/>
</config>

Step 2: Override Cart Controller:- 

  • Create a di.xml file at app/code/YourNamespace/YourModule/etc/frontend/di.xml to override the cart controller:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Controller\Cart\Add">
        <plugin name="yourmodule_cart_add" type="YourNamespace\YourModule\Plugin\Cart\AddPlugin" sortOrder="10"/>
    </type>
</config>


Step 3: Create a Plugin:-

  • Create the plugin class at app/code/YourNamespace/YourModule/Plugin/Cart/AddPlugin.php:
<?php
namespace YourNamespace\YourModule\Plugin\Cart;

class AddPlugin
{
    public function aroundExecute(
        \Magento\Checkout\Controller\Cart\Add $subject,
        \Closure $proceed
    ) {
        $productId = (int)$subject->getRequest()->getParam('product');
        $params = $subject->getRequest()->getParams();

        // Check if the product is a group product
        if ($this->isGroupProduct($productId)) {
            // Loop through the selected products and add them to the cart
            foreach ($params['super_group'] as $childProductId => $qty) {
                if ($qty > 0) {
                    $subject->getRequest()->setParam('product', $childProductId);
                    $subject->getRequest()->setParam('qty', $qty);
                    $proceed();
                }
            }
            return $subject;
        }

        return $proceed();
    }

    protected function isGroupProduct($productId)
    {
        // Implement your logic to check if the product is a group product
        // Return true if it's a group product, otherwise return false
    }
}
How to Allow Individual Group Product Selection for Separate Cart Addition - magento source24

Step 4: Clear Cache and Test:-

  • Clear the Magento cache:
php bin/magento cache:clean
Test the functionality on the storefront by adding a group product to the cart and checking if the individual simple products are added separately.Keep in mind, this is a basic guide, and you might need to adjust the code according to your specific needs. Additionally, this customization might affect the default behavior of adding group products, so thorough testing is essential before deploying it to a production environment.


Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post