dolibarr 25.0.0-alpha
ProductCombination Class Reference

Class ProductCombination Used to represent the relation between a product and one of its variants. More...

Public Member Functions

 __construct (DoliDB $db)
 Constructor.
 
 fetch ($rowid, $ref_product_child='')
 Retrieves a ProductCombination by its rowid or by the ref of its child product.
 
 fetchCombinationPriceLevels ($fk_price_level=0, $useCache=true)
 Retrieves combination price levels.
 
 saveCombinationPriceLevels ($clean=1)
 Retrieves combination price levels.
 
 fetchByFkProductChild ($productid, $donotloadpricelevel=0)
 Retrieves information of a variant product and ID of its parent product.
 
 fetchAllByFkProductParent ($fk_product_parent, $sort_by_ref=false)
 Retrieves all product combinations by the product parent row id.
 
 countNbOfCombinationForFkProductParent ($fk_product_parent)
 Retrieves all product combinations by the product parent row id.
 
 create ($user)
 Creates a product attribute combination.
 
 update (User $user)
 Updates a product combination.
 
 delete (User $user)
 Deletes a product combination.
 
 deleteLinksByProduct ($fk_product)
 Delete every combination row referencing a product, without deleting any product.
 
 deleteByFkProductParent ($user, $fk_product_parent)
 Deletes all product combinations of a parent product.
 
 updateProperties (Product $parent, User $user)
 Updates the weight of the child product.
 
 fetchByProductCombination2ValuePairs ($prodid, array $features)
 Retrieves the combination that matches the given features.
 
 resolveAttributeValueId (&$arrayrecord, $arrayfield, $pos)
 Resolves the id of an attribute value from its ref, for the 'compute' rule of the import of the attribute/value links.
 
 getUniqueAttributesAndValuesByFkProductParent ($productid)
 Retrieves all unique attributes for a parent product (filtered on its 'to sell' variants)
 
 createProductCombination (User $user, Product $product, array $combinations, array $variations, $price_var_percent=false, $forced_pricevar=false, $forced_weightvar=false, $forced_refvar=false, $ref_ext='', $clone_categories=false, $forcedcombination=null)
 Creates a product combination.
 
 copyAll (User $user, $origProductId, Product $destProduct)
 Copies all product combinations from the origin product to the destination product.
 
 getCombinationLabel ($prod_child)
 Return label for combinations.
 

Detailed Description

Class ProductCombination Used to represent the relation between a product and one of its variants.

Example: a product "shirt" has two variants "shirt XL white" and "shirt XL grey". This is represented with two ProductCombination objects:

  • One for "shirt XL white":
    • $object->fk_product_parent ID of the Product object "shirt"
    • $object->fk_product_child ID of the Product object "shirt XL white"
  • Another for "shirt XL grey":
    • $object->fk_product_parent ID of the Product object "shirt"
    • $object->fk_product_child ID of the Product object "shirt XL grey"

Definition at line 42 of file ProductCombination.class.php.

Constructor & Destructor Documentation

◆ __construct()

ProductCombination::__construct ( DoliDB $db)

Constructor.

Parameters
DoliDB$dbDatabase handler

Definition at line 137 of file ProductCombination.class.php.

References $conf.

Member Function Documentation

◆ copyAll()

ProductCombination::copyAll ( User $user,
$origProductId,
Product $destProduct )

Copies all product combinations from the origin product to the destination product.

Parameters
User$userObject user
int$origProductIdOrigin product id
Product$destProductDestination product
Returns
int >0 OK <0 KO

Definition at line 1301 of file ProductCombination.class.php.

References createProductCombination(), fetchAllByFkProductParent(), and getDolGlobalInt().

◆ countNbOfCombinationForFkProductParent()

ProductCombination::countNbOfCombinationForFkProductParent ( $fk_product_parent)

Retrieves all product combinations by the product parent row id.

Parameters
int$fk_product_parentId of parent product
Returns
int Nb of record

Definition at line 402 of file ProductCombination.class.php.

References getEntity().

◆ create()

ProductCombination::create ( $user)

Creates a product attribute combination.

Parameters
User$userObject user
Returns
int Return integer <0 if KO, >0 if OK

Definition at line 424 of file ProductCombination.class.php.

References getDolGlobalString(), and saveCombinationPriceLevels().

◆ createProductCombination()

ProductCombination::createProductCombination ( User $user,
Product $product,
array $combinations,
array $variations,
$price_var_percent = false,
$forced_pricevar = false,
$forced_weightvar = false,
$forced_refvar = false,
$ref_ext = '',
$clone_categories = false,
$forcedcombination = null )

Creates a product combination.

Check usages to find more about its use Format of $combinations array: array( 0 => array( attr => value, attr2 => value [...] ), [...] )

Parameters
User$userUser
Product$productParent Product
array<int,int>$combinations Attribute and value combinations.
array<int,array<int,array{weight:string|float,price:string|float}>>$variations Price and weight variations (example: $variations[fk_product_attribute][fk_product_attribute_value]['weight'])
bool | bool[]$price_var_percentIs the price variation value a relative variation (in %)? (it is an array if global constant "PRODUIT_MULTIPRICES" is on)
false | float | float[]$forced_pricevarValue of the price variation if it is forced ; in currency or percent. (it is an array if global constant "PRODUIT_MULTIPRICES" is on)
false | float$forced_weightvarValue of the weight variation if it is forced
false | string$forced_refvarValue of the reference if it is forced
string$ref_extExternal reference
bool$clone_categoriesAdd parent product categories to the created variant
?ProductCombination$forcedcombinationCombination to reconcile. Without it, the combination is looked up from its value set, which is ambiguous as long as the set is partial.
Returns
int Return integer <0 KO, >0 OK (id of the child product)

If there is an existing combination, then we update the prices and weight Otherwise, we try adding a random number to the ref

Definition at line 942 of file ProductCombination.class.php.

References $conf, dol_syslog(), getDolGlobalInt(), getDolGlobalString(), and price2num().

Referenced by copyAll().

◆ delete()

ProductCombination::delete ( User $user)

Deletes a product combination.

Parameters
User$userObject user
Returns
int Return integer <0 if KO, >0 if OK

Definition at line 499 of file ProductCombination.class.php.

◆ deleteByFkProductParent()

ProductCombination::deleteByFkProductParent ( $user,
$fk_product_parent )

Deletes all product combinations of a parent product.

Parameters
User$userObject user
int$fk_product_parentRowid of parent product
Returns
int Return integer <0 if KO, >0 if OK

Definition at line 584 of file ProductCombination.class.php.

References fetchAllByFkProductParent().

◆ deleteLinksByProduct()

ProductCombination::deleteLinksByProduct ( $fk_product)

Delete every combination row referencing a product, without deleting any product.

Used when the product is removed while the variants module is disabled: the rows would otherwise be refused by the foreign key on fk_product_parent.

Parameters
int$fk_productId of the product, as a parent or as a child
Returns
int<-1,1> -1 if KO, 1 if OK

Definition at line 532 of file ProductCombination.class.php.

References dol_syslog().

◆ fetch()

ProductCombination::fetch ( $rowid,
$ref_product_child = '' )

Retrieves a ProductCombination by its rowid or by the ref of its child product.

Note: $rowid must not be typed as int. The import engine resolves a foreign key by calling fetch('', $ref) and an empty string is not a numeric string in PHP 8.

Parameters
int | string$rowidID of the ProductCombination
string$ref_product_childRef of the child (variant) product, used when $rowid is empty
Returns
int<-1,1> -1 if KO, 0 if not found, 1 if OK

Definition at line 155 of file ProductCombination.class.php.

References dol_syslog(), fetchCombinationPriceLevels(), and getDolGlobalString().

◆ fetchAllByFkProductParent()

ProductCombination::fetchAllByFkProductParent ( $fk_product_parent,
$sort_by_ref = false )

Retrieves all product combinations by the product parent row id.

Parameters
int$fk_product_parentRowid of parent product
bool$sort_by_refSort result by product child reference
Returns
int|ProductCombination[] Return integer <0 KO

Definition at line 356 of file ProductCombination.class.php.

References getDolGlobalString(), and getEntity().

Referenced by copyAll(), and deleteByFkProductParent().

◆ fetchByFkProductChild()

ProductCombination::fetchByFkProductChild ( $productid,
$donotloadpricelevel = 0 )

Retrieves information of a variant product and ID of its parent product.

Parameters
int$productidProduct ID of variant
int$donotloadpricelevelAvoid loading price impact for each level. If PRODUIT_MULTIPRICES is not set, this has no effect.
Returns
int Return integer <0 if KO, 0 if product ID is not ID of a variant product (so parent not found), >0 if OK (ID of parent)

Definition at line 316 of file ProductCombination.class.php.

References $conf, fetchCombinationPriceLevels(), getDolGlobalString(), and getEntity().

◆ fetchByProductCombination2ValuePairs()

ProductCombination::fetchByProductCombination2ValuePairs ( $prodid,
array $features )

Retrieves the combination that matches the given features.

Parameters
int$prodidId of parent product
array<int|string,int|string>$features Format: [$attr] => $attr_val
Returns
false|ProductCombination False if not found
Parameters
mixed$vFeature information of a product.
Returns
bool

Definition at line 744 of file ProductCombination.class.php.

◆ fetchCombinationPriceLevels()

ProductCombination::fetchCombinationPriceLevels ( $fk_price_level = 0,
$useCache = true )

Retrieves combination price levels.

Parameters
int$fk_price_levelThe price level to fetch, use 0 for all
bool$useCacheTo use cache or not
Returns
int<-1,1> -1 if KO, 1 if OK

for auto retrocompatibility with last behavior

Definition at line 223 of file ProductCombination.class.php.

References ProductCombinationLevel\createFromParent(), and getDolGlobalString().

Referenced by fetch(), and fetchByFkProductChild().

◆ getCombinationLabel()

ProductCombination::getCombinationLabel ( $prod_child)

Return label for combinations.

Parameters
int$prod_childid of child
Returns
string combination label

Definition at line 1356 of file ProductCombination.class.php.

Referenced by updateProperties().

◆ getUniqueAttributesAndValuesByFkProductParent()

ProductCombination::getUniqueAttributesAndValuesByFkProductParent ( $productid)

Retrieves all unique attributes for a parent product (filtered on its 'to sell' variants)

Parameters
int$productidParent Product rowid
Returns
array<object{id:int,ref:string,label:string,values:ProductAttributeValue[]}> Array of attributes

Definition at line 867 of file ProductCombination.class.php.

◆ resolveAttributeValueId()

ProductCombination::resolveAttributeValueId ( & $arrayrecord,
$arrayfield,
$pos )

Resolves the id of an attribute value from its ref, for the 'compute' rule of the import of the attribute/value links.

A value ref is only unique for a given attribute, and the fetchidfromref rule of the import engine resolves every column without any context, so the attribute ref has to be read from the same file line. The 'pac2v.fk_prod_attr' alias is hard coded here on purpose: it is the alias declared by the import dataset of modVariants, and the engine has no way to pass it.

Parameters
array<int,array{val:string,type:int}>$arrayrecord Line read from the file, by reference (the signature of the engine requires it)
array<string,int>$arrayfield Field alias => column index of the line
int$posColumn index of the value ref
Returns
int Id of the attribute value, 0 on error

Definition at line 801 of file ProductCombination.class.php.

References dol_syslog().

◆ saveCombinationPriceLevels()

ProductCombination::saveCombinationPriceLevels ( $clean = 1)

Retrieves combination price levels.

Parameters
int$cleanLevels of PRODUIT_MULTIPRICES_LIMIT
Returns
int Return integer <0 KO, >0 OK

Definition at line 270 of file ProductCombination.class.php.

References $conf.

Referenced by create(), and update().

◆ update()

ProductCombination::update ( User $user)

Updates a product combination.

Parameters
User$userObject user
Returns
int Return integer <0 KO, >0 OK

Definition at line 458 of file ProductCombination.class.php.

References getDolGlobalString(), saveCombinationPriceLevels(), and updateProperties().

◆ updateProperties()

ProductCombination::updateProperties ( Product $parent,
User $user )

Updates the weight of the child product.

The price must be updated using Product\updatePrices. This method is called by the update() of a product.

Parameters
Product$parentParent product
User$userObject user
Returns
int >0 if OK, <0 if KO

Definition at line 624 of file ProductCombination.class.php.

References getCombinationLabel(), getDolGlobalInt(), and getDolGlobalString().

Referenced by update().


The documentation for this class was generated from the following file: