I would suggest doing this
Lets say your basket variable has your shopping cart value in the cookie
basket = "Product1.ID|Quantity/Product2.ID|Quantity"
Convert it to a hash by doing
Hash[basket.split("/").map{|p| p.split("|")}]
Now you will get a hash with product id as the key and quantity as value
products = {"Product1.ID" => "Quantity", "Product2.ID" => "Quantity"}
products.each do |product_id, quantity|
cost = Product.find(product_id).price * quantity.to_i
end