As you are saving the data in cookie it expects only string value to save in it.
So its difficult to maintain the updated quantity as we can't maintain the index of product_id. So if you implement the same in the datatable and convert the datatable to string value then it will very easy to implement.
You need to retrieve the value from cookie and insert into the datatable and then insert or update or delete the record based on product_id and then looping through eachrow save the data in comma separated vale as string.