Add
int CSaleBasket::Add( array arFields );
The method Add adds a product item to the basket unless it is already added, or updates the product properties (increases its quantity) is this product has already been added to the basket.
The array arFields should contain all the product properties required by the e-Store module because this module is independent of the other modules in the system.
You can add products to basket from any page, static or dynamic, from any
place in the code (e.g. from the Commercial Catalog). Some modules
provide wrapper functions which helps to add products to basket. For example,
the Commercial Catalog module exports function Add2Basket($PRICE_ID, $QUANTITY)
.
Parameters
Parameter | Description |
---|---|
arFields | Associated array containing new parameters of basket item. The following keys are possible:
|
Returned values
Returns the ID of the new basket item.
Example
<? if (CModule::IncludeModule("sale")) { $arFields = array( "PRODUCT_ID" => 51, "PRODUCT_PRICE_ID" => 0, "PRICE" => 138.54, "CURRENCY" => "USD", "WEIGHT" => 530, "QUANTITY" => 1, "LID" => LANG, "DELAY" => "N", "CAN_BUY" => "Y", "NAME" => "Lather suitcase", "CALLBACK_FUNC" => "MyBasketCallback", "MODULE" => "my_module", "NOTES" => "", "ORDER_CALLBACK_FUNC" => "MyBasketOrderCallback", "DETAIL_PAGE_URL" => "/".LANG."/detail.php?ID=51" ); $arProps = array(); $arProps[] = array( "NAME" => "Colour", "CODE" => "color", "VALUE" => "Black" ); $arProps[] = array( "NAME" => "Size", "VALUE" => "1.5 x 2.5" ); $arFields["PROPS"] = $arProps; CSaleBasket::Add($arFields); } ?>
Basket actualization callback function
If the basket actualization callback function is specified, it is called
every time the basket is being queried. This allows to properly and fully update
parameters of products in the basket. For example, is the product price or
availability becomes changed after adding it to the basket, the callback
function can properly update parameters of the basket items. The CALLBACK_FUNC
field contains only the name of the callback function. Some modules provide the
preset callback functions. For example
the Commercial Catalog module provides function
CatalogBasketCallback($PRODUCT_ID, $QUANTITY = 0)
.
array CALLBACK_FUNC( int PRODUCT_ID [, int QUANTITY] );
Return Values
The callback function must return an array of the same format as the function CSaleBasket::Add takes on input. If the callback function returns an empty array, the product is considered unavailable.
Parameters
- PRODUCT_ID
- The ID of the product currently in the basket.
- QUANTITY
- Number of the product items.
Example
<? function MyBasketCallback($PRODUCT_ID, $QUANTITY = 0) { $arResult = array(); $iProductQuantity = GetProductQuantity($PRODUCT_ID); if ($iProductQuantity<=0) // not in stack, return empty array return $arResult; $arResult = array( "PRODUCT_PRICE_ID" => 0, "PRICE" => 125.2, "CURRENCY" => "USD", "WEIGHT" => 530, "NAME" => "Leather suitcase", "CAN_BUY" => "Y" ); if (IntVal($QUANTITY)>0 && ($iProductQuantity-$QUANTITY)<0) { // number of products in stock is less // than the basket specifies, so // decrease the quantity $arResult["QUANTITY"] = $iProductQuantity; } return $arResult; } ?>
Ordering callback function
If the ordering callback function is specified, it is called when a
product in the basket is being ordered. For example, the callback function can
track quantity of the currently in-stock product items and decrease the number
of product items in the basket accordingly. The ORDER_CALLBACK_FUNC
field contains only the name of the callback function. Some modules provide the
preset callback functions. For example
the Commercial Catalog module provides function
CatalogBasketOrderCallback($PRODUCT_ID, $QUANTITY)
.
void ORDER_CALLBACK_FUNC( int PRODUCT_ID, int QUANTITY );
Returned values
No return values.
Parameters
- PRODUCT_ID
- The ID of the product currently in the basket.
- QUANTITY
- Number of the product items.
Example
<? function MyBasketOrderCallback($PRODUCT_ID, $QUANTITY) { UpdateProductQuantity($PRODUCT_ID, $QUANTITY); } ?>