Documentation

Adding items to shopping cart

Attention! It's recommended to use methods to handle shopping cart for: D7 core.

Catalog module includes wrapper functions for handling sales module:

Add2Basket


Attention! Function Add2Basket has deprecated and is no longer supported starting from catalog 16.5.0.


int Add2Basket(  int PRICE_ID[,
	int QUANTITY = 1[,
	array arRewriteFields = array()[,
	array arProductParams = array()]]]
);

Function adds a product to shopping cart with PRICE_ID in amount of QUANTITY unit (single unit by default) and returns product item code inside shopping cart. This function is a wrapper for CSaleBasket::Add, but is allocated in the Catalog module.

Function parameters

Parameter Description
PRICE_IDProduct price ID.
QUANTITY Number of added product items (by default - single item)
arRewriteFields Array with keys for shopping cart class matches with the same-name array of class CSaleBasket, except the key module_id.
arProductParams Array, containing list of product properties, added to the shopping cart. Keys:
  • NAME - property title;
  • CODE - property symbolic code;
  • VALUE - property value;
  • SORT - sorting.

Note: array items have continuous numbering, starting from zero, prior to version 11.0.2.

Returned value

Code for entry inserted to shopping cart or False on error.

Possible errors:

  • Online store module is not installed;
  • search bot attempt to add a product to the shopping cart (only with available Web Analytics module and bot calculation based on UserAgent);
  • price or price types are missing for specified quantity and used for purchase by user;
  • access permissions are missing for reading an iblock element with the price added to shopping cart.

Example

<?
if (CModule::IncludeModule("catalog"))
{
	if (($action == "ADD2BASKET" || $action == "BUY") && IntVal($PRICE_ID)>0)
	{
		Add2Basket(
			$PRICE_ID,
			2,
			array(),
			array(
				array("NAME" => "Цвет", "CODE" => "CLR", "VALUE" => "красный"),
				array("NAME" => "Размер", "VALUE" => "25")
			)
		);
		if ($action == "BUY")
			LocalRedirect("basket.php");
	}
}
?>

Add2BasketByProductID


Attention! Function Add2BasketByProductID has deprecated and is no longer supported starting from catalog module version 16.5.0. Use the method \Bitrix\Catalog\Product\Basket::addProduct.


int Add2BasketByProductID( int PRODUCT_ID[, int QUANTITY = 1[, array arProductParams = array()]]
);

After version 11.5.7, a new call variant has become available (old call retains its operability as well):

int Add2BasketByProductID( $PRODUCT_ID, $QUANTITY = 1, $arRewriteFields = array(), $arProductParams = false);

Function adds to shopping cart a product with PRODUCT_ID and QUANTITY items (single item by default) and returns product item code inside shopping cart. This function is a wrapper for CSaleBasket::Add, but allocated in the Commercial Catalog module.

Function parameters

Parameter Description Available from version
PRODUCT_IDProduct code.
QUANTITY Amount of added product items (by default - single item)
arRewriteFields Array with keys for shopping cart class matches to identically names array of class CSaleBasket. 11.5.7
arProductParams Array, containing list of product properties added to shopping cart. Keys:
  • NAME - property name;
  • CODE - property character code;
  • VALUE - property value;
  • SORT - сортировка.

Note: prior to version 11.0.2 array items were numbered incorrectly, starting from zero.

11.5.7

Note: in case when using the function, you need to pass the array arRewriteFields, but without passing the array arProductParams, product property array must be passed empty, for example:
Add2BasketByProductID(
	$PRODUCT_ID, 
	$QUANTITY, 
	array('ORDER_ID' => intval($arItems['ORDER_ID'])), 
	array()
);

Returned value

Entry code, inserted to shopping cart or False otherwise.

Possible errors:

  • Online store module is not installed;
  • search bot attempt to add a product to the shopping cart (only with available Web Analytics module and bot calculation based on UserAgent);
  • price or price types are missing for specified quantity and used for purchase by user;
  • access permissions are missing for reading an iblock element with the price added to shopping cart.

Examples

<?
if (CModule::IncludeModule("catalog"))
{
	if (($action == "ADD2BASKET" || $action == "BUY") && IntVal($PRODUCT_ID)>0)
	{
		Add2BasketByProductID(
			$PRODUCT_ID,
			2,
			array(
				array("NAME" => "Цвет", "CODE" => "CLR", "VALUE" => "красный"),
				array("NAME" => "Размер", "VALUE" => "25")
			)
		);
		if ($action == "BUY")
			LocalRedirect("basket.php");
	}
}
?>

Note: example was created for a legacy entry format. Compatibility with the new format is preserved.


© «Bitrix24», 2001-2024
Up