Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:35:36+00:00 2026-06-11T10:35:36+00:00

I made a system a while back which involves a basic shopping cart. The

  • 0

I made a system a while back which involves a basic shopping cart. The cart is a very simple OOP system which adds items by 1 quantity at a time. Here is the function for adding an item to the cart.

    function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ 

    if($this->itemqtys[$itemid] > 0)
            { // the item is already in the cart..
      // so we'll just increase the quantity

        $this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
        $this->_update_total();
    } else {
        $this->items[]=$itemid;
        $this->itemqtys[$itemid] = $qty;
        $this->itemprices[$itemid] = $price;
        $this->iteminfo[$itemid] = $info;
    }
    $this->_update_total();
} 

The problem with this is some of the products have variations (sizes, colours etc..) but when they chose a different variation in the same order, the products are not added correctly – all of the same product falling under one variation.

I thought it would be an idea to use multidimensional arrays, or arrays within arrays, to produce something like:

$this->iteminfo[$itemid][$var];

Getting the var from the product is not a problem, just im struggling with how to add the product to the cart with its variation, and updating the quantity of this product/variation combination only when the variation is added again. If a different variation is added, it would add the product in a new entry in the cart?

Hope this makes sense :/ Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:35:38+00:00Added an answer on June 11, 2026 at 10:35 am

    I would suggest using a single dimension(al) array, but rather than filling it with plain old ints and strings, place a object within it. It will be much more robust that way, and you can easily cycle through to get whatever you need – as well as being able to expand the object out further in the future.

    Something like this would even work:

    class itemToBuy
    {
        public $itemID;
        public $qty;
        public $price;
        public $info;
        public $whoKnowsWhat;
    }
    
    class yourShoppingCart
    {
        function add_item($newItem)
        {
            $itemInserted==false;
            $itemsInCart=count($this->itemqtys);
            for($i=0;$i<$itemsInCart;$i++)
            {
                if($this->itemqtys[$i]->itemID==$newItem->itemID)
                {
                    $this->itemqtys[$i]->qty+=$newItem->qty;
                    $itemInserted=true;
                }
            }
            if(!itemInserted)
            {
                $this->itemqtys[]=$newItem;
            }
            $this->_update_total();
        } 
    }
    

    Then just pass the object instances of itemToBuy rather than the arrays you are passing it at the moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a simple C# WinForms app, which makes a screen-capture using System; using
I have an application in which we use a hand-made build system.The reason for
I've made myself a simple event system in python, and I found that the
Here's a simple events system I've made using multimaps; When I use the CEvents::Add(..)
I am trying to make a simple tile system for my game. I made
I made a program which converts n decimal numbers sk into other numerical system
A while back I made a Model class. I made several ModelForms for it
I have made a description system, but all which doesn't work is my search
Currently I'm working on a GUI system, and I've made a GUIButton object which
I have come across an annoying problem. I have made a system and now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.