barcodebuddy
barcodebuddy copied to clipboard
Userfield as variable
Hi !
I forked your amazing repository, because I wish use a value of a Userfield in the "incl/processing.inc.php"
But it was unsuccessful...
How could I create a variable to use this userfield:
And use it in "incl/processing.inc.php". I tried to modified "incl/api.inc.php", and "db.inc.php", but I don't modify as expected, because it doesn't work...
Could you help me ?
Thank you !!!
Hi, you will probably also need to edit the object that is created in the incl/api.php file: https://github.com/Forceu/barcodebuddy/blob/master/incl/api.inc.php
Hi, I tried to work on it, but unsuccessful...
My goal is to get this value:
![Capture d’écran 2022-02-01 à 12 11 07](https://user-images.githubusercontent.com/25400986/151958564-bddebae1-c23c-44d8-bb21-890e53a76f40.png)
In file api.inc.php, I added:
const API_USERFIELDS = 'userfields/products';
class GrocyProduct {
public $quantiteAConsommer;
public static function parseUserfieldsInfo(array $infoArray): GrocyProduct {
checkIfNumeric($infoArray["id"]);
$result = new GrocyProduct();
$result->quantiteAConsommer = $infoArray["QuantiteAConsommer"];
return $result;
}
class API {
public static function getUserfields(int $productId): ?GrocyProduct {
$url = API_USERFIELDS . "/" . $productId;
$result = null; // Assure assignment in event curl throws exception.
$curl = new CurlGenerator($url);
try {
$result = $curl->execute(true);
} catch (Exception $e) {
self::processError($e, "Could not lookup Grocy product info");
}
if ($result != null) {
if (isset($result["userfields"])) {
return GrocyProduct::parseUserfieldsInfo($result);
} else {
return null;
}
}
return null;
}
}
In file processing.inc.php, I added:
$quantiteAConsommer = API::getUserfields($productInfo->id);
And I try to display $quantiteAConsommer
in the log with:
$log = new LogOutput("Consuming " . $quantiteAConsommer, EVENT_TYPE_ADD_KNOWN_BARCODE);
But it stay empty...