Assume we want to add a configurable product that has custom options. Lets took an example for suppose our product is a T-Shirt and its coming colored version. Each color has its different option id and option value.
The same thing goes with the configurable product also like create an instance of a cart and then initialize it that will create product collection.
Programmatically Adding Configurable product to cart is different from simple type product. Here in an array, we have to pass options array also. This options array contains the chosen option id and option value for the product.
// Get cart and product collection
$cart = Mage::getModel('checkout/cart');
$productCollection = Mage::getModel('catalog/product')->load($productId);
// Add configurable product to cart
array( 'product_id' => $productId,
'qty' => 1,
'options' => array( $optionId => $optionValue));
// Save cart
That’s it done with adding configurable product.