I wanted to give back for this brilliant piece of code IZGHITU provided that helped me tremendously. The problem I faced is that I needed this coded to work for multiple products utilizing the same custom field name and his code snippet was hard coded for one product. I am not that great but a couple hours later it is dynamic for multiple products. Just change the 'fieldname' in the first query to match your needs. You can also change 'fieldtype' and 'type' to further suit your needs. If anyone can provide constructive criticism I'd appreciate it but for now this works for me and for many products instead of just one even if you delete a product and add it again since it will find the id for us now. For me I wanted to see the Location Name in the admin area.
if($_REQUEST['getAll'] == '1')
{
$newAll = [];
$gotCustomFieldID = [];
// Get fieldid for 'Location Name' for every product
// Change 'Location Name' to suit your custom field needed
$getCustomFieldID = Capsule::table('tblcustomfields')
->select('id')
->where('tblcustomfields.type','product')
->where('tblcustomfields.fieldname','Location Name')
->where('tblcustomfields.fieldtype','text')
->get();
foreach ($getCustomFieldID as $data)
{
$gotCustomFieldID[] = $data->id;
}
foreach ($gotCustomFieldID as $customFieldID)
{
$all = Capsule::table('tblcustomfieldsvalues')
->join('tblhosting', 'tblcustomfieldsvalues.relid', '=', 'tblhosting.id')
->where('fieldid', $customFieldID )
->where('userid', $_REQUEST['userid'] )
->get();
$all = json_decode( json_encode( $all ) , true );
foreach($all as $a)
{
$newAll[$a['relid']] = $a['value'];
}
}
ob_clean();
echo json_encode(array( "all" => $newAll ));
die;
}