October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

PHP Shopping Cart: How to Keep Different Product Sizes in Separate Cart Lines

A PHP cart keyed only by product ID can merge different sizes. Distinguish lines with a product-and-size key or store and compare the selected options.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To show the same product in two sizes as separate cart entries, make the selected size part of each cart line’s identity—or store it as a variant option and distinguish lines by their options. If your session cart is keyed only by product_id, adding another size will find the existing entry and increase its quantity instead.

Why the example merges different sizes

The SitePoint discussion’s example stores cart entries in $_SESSION['item_cart'], keyed by product ID. When an item is added, the code checks whether that ID is already present and increments the existing line’s quantity. Because size does not contribute to the lookup, a size 9 and a size 11 of the same product are treated as one line.

As an Amazon Associate I earn from qualifying purchases.

The question was raised in a SitePoint forum thread opened December 2, 2017. Participants suggested either combining product ID and size in the key, or storing size as an option. The thread does not include a completed implementation or verify the resulting cart output. Read the SitePoint discussion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose how cart lines represent variants

Approach How it distinguishes lines When it fits Trade-off
Composite key Use a combination of product ID and size (or another unique combination) as the cart-line key. A simple cart where one option, such as size, distinguishes the variants. As more attributes are added, the key must represent their combination consistently.
Options stored on the line Keep product identity and store selected attributes such as size, color, or fit as line options. A cart that needs to represent different kinds or combinations of product attributes. Cart lookup must compare the selected options as well as the product; matching product ID alone is not enough.

These are design directions from the forum, not tested implementations. ZooKeeper, a forum participant, described options as more adaptable to products with attributes such as color and fit. Another participant, droopsnoot, suggested keying by product ID plus size or another unique combination. Neither comment establishes which approach is right for every store.

What the cart should distinguish

A cart line represents a particular purchasable selection, not merely a catalog product. If two selections differ in an attribute that matters to the order—such as size—they need separate lines. If the same product and same selected options are added again, the cart can increment that matching line’s quantity.

  • Same product, different size: keep separate lines.
  • Same product, same size: combine into one line and increase its quantity, if that is the cart’s intended behavior.
  • More attributes: compare the full selected combination, such as size and color, rather than only one attribute.
  • Products without a size: represent the absence of that option consistently; the forum reply mentions a placeholder size, but does not prescribe a particular value or implementation.

Keep stock and variant identity in view

A forum participant also raised the possibility of using size-specific SKUs to represent variants and manage stock. That is a participant’s observation, not a requirement established by the thread. The important modeling question is whether each size or other option is separately sellable or stocked. If so, the cart’s line identity and the store’s variant or SKU model should identify that specific selection, rather than collapsing all sizes into one product-level quantity.

The 2017 discussion does not specify an inventory model, database schema, or tested PHP solution, so it cannot determine how a particular store should connect cart options to its stock records.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What the original thread does—and does not—answer

The thread identifies why the sample merges sizes and presents two ways to think about distinguishing them: composite keys and stored options. It does not supply the requested final output, a working code change, or test results. Treat it as a discussion of the data-modeling problem, not as a production-ready implementation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.