0

Currently, I am having an issue with retrieving group parent product_id using PHP external script wich file resides under /pub directory:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productRepository = 
$objectManager->get('\Magento\Catalog\Model\ProductRepository');

$typeGroup = $objectManager->get('\Magento\GroupedProduct\Model\Product\Type\Grouped');

//child product_id
$simple_child_associated_to_group_product_id = 13884;
$product = $productRepository->getById($simple_child_associated_to_group_product_id);

//this returns the 
//row_id of the parent not the product_id
$groupParentIds = $typeGroup->getParentIdsByChild($product->getId());


//shortens the array by one element and store it to a variable
$groupParentId = array_shift($groupParentIds); 

//display group parent id
echo $groupParentId; 

You can confirm the echoed value is the row_id value of getParentIdsByChild() using this query in your M2 Database:

SELECT * FROM `catalog_product_entity` WHERE `row_id` = 'put $groupParentId value here'

Take note of the entity_id:23526

enter image description here To confirm that the returned value of $groupParentId is truly the parent product of the child simple... you can confirm this by:

log-in to your admin panel, go to catalog > products, then search the parent product via ID filter with value of 23526 enter image description here

click the row to open the product info. and check the associated simple products in the Grouped Products section.

  • did you try var_dump($groupParentIds) ? – Mohit chauhan Mar 27 '19 at 10:11
  • @Mohitchauhan yes sir I tried and it returns the row_id of a product. I checked the native Magento 2 getParentIdsByChild() method equivalent SQL query: SELECT catalog_product_link.product_id, catalog_product_link.linked_product_id FROM catalog_product_link WHERE (linked_product_id IN('13884')) AND (link_type_id = 3) and confirmed that the product_id column of the catalog_product_link table is equivalent to the row id of the catalog_product_entity – Eubie Aluad Mar 27 '19 at 10:17
  • So this row_id is actually product_id of group product. now what else you want here ? – Mohit chauhan Mar 27 '19 at 10:20
  • @Mohitchauhan I basically wanted to retrieve the entity_id or product_id of a product given that I already have it's row_id. I already tried loading the product using product repository getById(row_id) however it returns me incorrect data. The product repository expects the id to be entity_id or product_id of a product. – Eubie Aluad Mar 27 '19 at 10:22
  • Didn't get you. you already have product_id as row_id. – Mohit chauhan Mar 27 '19 at 10:25
0

@Mhitchauhan answered my question by using this line of codes in my PHP external script file:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager 
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); 
$connection = $resource->getConnection(); 
$tableName = $resource->getTableName('catalog_product_entity'); //gives table name with prefix 
//Select Data from table 
$sql = "Select * FROM " . $tableName ." where row_id='$row_id'"; 
$result = $connection->fetchAll($sql); 
var_dump($result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.