Script development: Difference between revisions

Jump to navigation Jump to search
m
mNo edit summary
Line 40: Line 40:
Note that in this section, you can use the variable $db that is the resource to access Dolibarr database and is already initialized. The $conf object that contains Dolibarr configuration is also available.
Note that in this section, you can use the variable $db that is the resource to access Dolibarr database and is already initialized. The $conf object that contains Dolibarr configuration is also available.


=== Example to insert a product ===
== Examples ==
For example to insert a product in the database Dolibarr, you can place the following code:
Some code examples (to create orders, products...) are available into directory '''/dev/samples/'''.
<source lang="php">
// Include mother classes for user and product
require_once(DOL_DOCUMENT_ROOT."/user.class.php");
require_once(DOL_DOCUMENT_ROOT."/product.class.php");
 
// Create a new user instance
$user=new User($db);
 
// Create a new product instance
$myproduct=new Product($db);
 
// Define properties of user
$user->id = 0;
 
// Define properties of product
$myproduct->ref                = '1234';
$myproduct->libelle            = 'libelle';
$myproduct->price              = '10';
$myproduct->price_base_type    = 'HT';
$myproduct->tva_tx            = '19.6';
$myproduct->type              = 0;
$myproduct->status            = 1;
$myproduct->description        = 'Description';
$myproduct->note              = 'Note';
$myproduct->weight            = 10;
$myproduct->weight_units      = 0;
 
// Create product in database
$idproduct = $myproduct->create($user);
 
// Error management
if ($idproduct < 0) dol_print_error($db,$myproduct->error);
else print "Product $idproduct created.\n";
</source>

Navigation menu