Adding Group Products Individually Programmatically:-
Hello magento friends in this blog post i'm share with you to Enhancing Cart Functionality in Magento 2: Adding Group Products Individually Programmatically. To add group products into the cart individually in Magento 2 programmatically, you'll need to create a custom module or use existing functionality to modify the cart behavior. Here's a step-by-step guide on how to achieve this:
Step 1: Create a Custom Module (if not already created):-
If you don't have a custom module already, create one by following these steps:
- Create a folder structure for your module inside the app/code directory. For example, if your module is named "CustomCart," the directory structure should be app/code/YourNamespace/CustomCart.
- Create the registration.php file inside the CustomCart folder:
app/code/YourNamespace/CustomCart/registration.php
Add the following content to registration.php:
<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'YourNamespace_CustomCart',__DIR__);
app/code/YourNamespace/CustomCart/etc/module.xml
Add the following content to 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_CustomCart" setup_version="1.0.0"/>
</config>
Step 2: Create a Custom Cart Controller:-
app/code/YourNamespace/CustomCart/Controller/Cart/AddGroupProduct.php
Add the following content to AddGroupProduct.php:-
<?php
namespace YourNamespace\CustomCart\Controller\Cart;
use Magento\Framework\App\Action\Context;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Cart;
use Magento\Framework\Controller\Result\JsonFactory;
class AddGroupProduct extends \Magento\Framework\App\Action\Action
{
protected $productRepository;
protected $cart;
protected $resultJsonFactory;
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
Cart $cart,
JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->cart = $cart;
$this->resultJsonFactory = $resultJsonFactory;
}
public function execute()
{
$productId = $this->getRequest()->getParam('product_id');
$productQty = $this->getRequest()->getParam('qty', 1);
try {
$product = $this->productRepository->getById($productId);
if ($product->getTypeId() === 'grouped') {
$associatedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
foreach ($associatedProducts as $associatedProduct) {
$this->cart->addProduct($associatedProduct, ['qty' => $productQty]);
}
$this->cart->save();
} else {
throw new \Exception('The specified product is not a grouped product.');
}
$result = ['success' => true, 'message' => __('Grouped products added to the cart.')];
} catch (\Exception $e) {
$result = ['success' => false, 'message' => $e->getMessage()];
}
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($result);
}
}
Step 3: Create a Frontend Form or AJAX Request:-
You'll need to create a frontend form or use AJAX to send requests to the AddGroupProduct controller to add group products to the cart individually. Remember to update the namespace and module name according to your specific module's naming conventions.
Once you've completed these steps, you should be able to add group products individually to the cart in Magento 2 programmatically by accessing the custom controller you created.