You are using a compound key (item_id + color) but storing it by item_id alone. That won't work. Your cart is a hash, which means it won't store duplicates. Your add_item code checks to see if that item_id is there in the cart already before adding it, which will also prevent the same item with two colors from being added twice.

Frankly, this is so obvious that I think you must be asking a different question. Are you asking what a better data structure would be? I'd suggest you give each color a unique item_id, and create a higher-level entity (say, "product") to represent several items that are color variations of the same basic thing. Then your item_id will be unqiue and can be stored in the way you're doing it now.


In reply to Re: Need duplicate item entries in shopping cart by perrin
in thread Need duplicate item entries in shopping cart by b310

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.