Hi,
onBeforeCartSave is called by the save function in class.cart
If I write such code somewhere:
$cart = new stdClass();
$cartClass->save($cart);
it will save a new cart to the database with nothing in it.
And when the onBeforeCartSave event will be called by the save function, you will have an object with attributes added automatically to it, like the session_id or user_id, the cart_type, etc but you won't have an cart_products attribute at all since the code calling the save function won't provide it.
That doesn't mean that there is a problem with the code triggering onBeforeCartSave in the save function of class.cart, or even with the code calling the save function.
It's just that you can't trust that you'll always get the data in $element so you need to add extra checks for those cases.
For example, if you're missing the product_type and you need it, you can run such code:
$productClass = hikashop_get('class.product');
$productData = $productData = $productClass->get($cart_product->product_id);
echo $productData->product_type;