Hi,
That's because the product added to the cart is actually not a product but a variant. And variants are not directly linked to products.
So instead of :
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($product->product_id);
you should use:
$id = $product->product_id;
if(!empty($product->product_parent_id))
$id = $product->product_parent_id;
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($id);
And yes, you could use a custom field of the table "category" and of the type "text" to be able to provide the name to be displayed in the cart.
And in the last line, you can then change category_name to the column name of your custom field.