To get child product data for configurable products within the "checkout_cart_product_add_after" event, you can use the following approach.
Firstly create the events.xml file in your module
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="vendor_module_custom_observer" instance="Vendor\Module\Observer\CustomObserver" />
</event>
</config>
After then create an Observer and then add the following code in it.
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
class CustomObserver implements ObserverInterface
{
protected $cart;
protected $redirect;
protected $request;
protected $product;
protected $configurableproduct;
public function __construct(RedirectInterface $redirect, Cart $cart, RequestInterface $request, Product $product, Configurable $configurableproduct)
{
$this->redirect = $redirect;
$this->cart = $cart;
$this->request = $request;
$this->product = $product;
$this->configurableproduct = $configurableproduct;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$postValues = $this->request->getPostValue();
$productId = $postValues['product'];
$addProduct = $this->product->load($productId);
if ($addProduct->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)
{
$attributes = $postValues['super_attribute'];
$simple_product = $this->configurableproduct->getProductByAttributes($attributes, $addProduct);
}
}
}