If you are trying to include the above code directly in the index.php It will definitely error.
Bcoz the this pointer referenced here is Cart helper. That is unknown in the index.php.
If you want to insert some cart features in the index.php best method is to use the module.
You can find some modules similar to the mod_cartinfo (This modules will return the cart item details.) in public_html/modules/mod_cartinfo
you can find the module main file loads some php files that are required for the VM cart details.
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
if(!class_exists('VirtueMartCart')) require(JPATH_VM_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
Then use $cart istead of this->cart.
don't try to include this code portion in your index.php.It will load every time.
Use module concept for achieving this.try this
Hope this will help you..