I need to be sent the individual product IDs of Grouped Products.
By default, Magento sends a grouped product ID for all individual products.
Here, how to get individual product IDs of Grouped Products.
Helper Class:
public function getProductDatalayer($productId, $simpleProductId = null)
{
$productData = array();
$product = $this->_productRepository->getById($productId);
$productDefault = $this->_productRepository->getById($productId, false, self::DEFAULT_STORE_ID);
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$imageHelper = $objectManager->get(MagentoCatalogHelperImage::class);
$imageUrl = $imageHelper->init($product, 'product_base_image')->getUrl();
$simpleProdData = $this->getSimpleProductsSize($product);
$productData['id'] = $product->getId();
// $productData['product_id'] =
$size = !empty($size) ? $size : $simpleProdData['size'];
$productData['product_id'] = $this->getProductIdBySize($product, $size);
$writer = new Zend_Log_Writer_Stream(BP . '/var/log/test77777log');
$logger = new Zend_Log();
$logger->addWriter($writer);
$logger->info($productData['product_id']);
$logger->info($size);
$productData['name'] = $productDefault->getName();
$productData['price'] = $product->getPriceInfo()->getPrice('regular_price')->getValue();
$productData['brand'] = $productDefault->getResource()->getAttribute('tommy_label')->setStoreId(self::DEFAULT_STORE_ID)->getFrontend()->getValue($productDefault);
$productData['category'] = $this->getProductCategory($productDefault);
$productData['category_id'] = $this->getProductCategoryIds($productDefault);
$productData['variant'] = $productDefault->getResource()->getAttribute('color')->setStoreId(self::DEFAULT_STORE_ID)->getFrontend()->getValue($productDefault);
$productData['sale_status'] = ($this->getProductDiscountPercent($product) > 0) ? 'On Offer' : 'No Offer';
$productData['stock_status'] = ($product->isInStock() == 1) ? 'In Stock' : 'Out of Stock';
$productData['discount_percent'] = $this->getProductDiscountPercent($product);
$productData['product_image'] =$imageUrl;
$productData['discount_price'] = $product->getFinalPrice();
$productData['product_gender'] = $productDefault->getResource()->getAttribute('gender')->setStoreId(self::DEFAULT_STORE_ID)->getFrontend()->getValue($productDefault);
return $productData;
}
Ajax:
public function execute()
{
$requestParams = $this->getRequest()->getParams();
$items = array();
$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
$simple_productId = isset($requestParams['simple_product_id']) ? (int)$requestParams['simple_product_id'] : null;
try {
if (!$productId) {
$response = array(
'errors' => true,
'message' => 'Product not found.'
);
} else {
$items = $this->helper->getProductDatalayer($productId, $simple_productId);
if( isset($requestParams['event']) ) {
$datalayer_flow = array();
$datalayer_flow = $this->checkoutSession->getDatalayerFlow();
if( $requestParams['event'] == 'Add_To_Cart' ) {
$items['quantity'] = 1;
$items['size'] = $requestParams['size'];
$items['buy_flow'] = 'Normal Flow';
//flow handle
$datalayer_flow[$productId] = array(
'buy_flow' => 'Normal Flow'
);
//$this->checkoutSession->setDatalayerFlow($datalayer_flow);
} elseif ( $requestParams['event'] == 'Quickbuy_Add_To_Cart' ) {
$items['quantity'] = 1;
$items['size'] = $requestParams['size'];
$items['buy_flow'] = 'Quick Buy';
//flow handle
$datalayer_flow[$productId] = array(
'buy_flow' => 'Quick Buy'
);
//$this->checkoutSession->setDatalayerFlow($datalayer_flow);
} elseif ( $requestParams['event'] == 'productClick' ) {
$items['position'] = $requestParams['position'];
$datalayer_flow[$productId] = array(
'list' => $requestParams['list']
);
}
$this->checkoutSession->setDatalayerFlow($datalayer_flow);
}
$response = array(
'errors' => false,
'message' => 'success',
//'message' => $datalayer_flow,
'data' => $items
);
}
} catch (Exception $e) {
$response = array(
'errors' => true,
'message' => $e->getMessage()
);
}
$resultJson = $this->_resultJsonFactory->create();
return $resultJson->setData($response);
}
custom.phtml:
$.ajax({
url: url.build('/customgtm/ajax/datalayer'),
type: 'POST',
data: {event : 'productClick', product : productId, position : position, list : listresult},
dataType: 'json',
success: function (response) {
if( !response.errors ) {
dataLayer.push({ecommerce:null});
dataLayer.push({
'event': 'productClick',
'ecommerce': {
'click': {
'actionField': {'list': listresult},
'products': [response.data]
}
}
});
}
},
error: function(error) {
console.log(error);
}
});
});