the best way to store quantity in $_SESSION - php

I have the following function to add products to shopping cart:
function AddToCart($pid)
{
$_SESSION['products'][]['product_id'] = $pid;
}
What is the best way to store product quantity to the same array with the product id, since the array after the ['products'] generates automatically I'm confused how can I assign quantity there? Thank you for your help:)

Why not store all of the information you need at the same time? Here is an excerpt from my own cart:
// add item to cart
$_SESSION['cart'][] = array('product_id' => $product_id,
'cat_id' => $cat_id,
'title' => $title,
'default_img' => $default_img,
'price' => $valid_product['base_price'],
'weight' => $valid_product['weight'],
'length' => $valid_product['length'],
'width' => $valid_product['width'],
'height' => $valid_product['height'],
'surcharge' => $surcharge,
'quantity' => $quantity,
'prop_selection' => $prop_selection,
'po_id' => $po_id,
'stock' => $stock,
'shipper_id' => $valid_product['shipper_id']);

If I understand the question correctly, when AddToCart() is called for a $pid that is already in the array, do you just want it to increment a quantity property for that product? Seems to me you just will need to use an associative array for $_SESSION['products'] instead of a numerically indexed array like you are now:
function AddToCart($pid, $quantity=1) {
if(is_array($_SESSION['products']) && array_key_exists($pid, $_SESSION['products'])) {
$_SESSION['products'][$pid]['quantity'] += $quantity;
} else {
$_SESSION['products'][$pid] = array(
'product_id' => $pid,
'quantity' => $quantity
);
}
}

Related

creating session array in my shopping cart not functioning

hello I am trying to create a simple shopping cart for my school project the problem I have was the session array is not creating and array with information. So I can't add items on my cart. here is my code.
if(isset($_POST['add_to_cart'])) {
if(isset($_SESSION['cart'])) {
$session_array_id = array_column($_SESSION['cart'], 'id');
if(!in_array($_GET['id'], $session_array_id)) {
$session_array = array(
'id' => $_GET['id'],
'name' => $_POST['name'],
'price' => $_POST['price'],
'quantity' => $_POST['quantity']
);
$_SESSION['cart'][0]= $session_array;
}
}else {
$session_array = array(
'id' => $_GET['id'],
'name' => $_POST['name'],
'price' => $_POST['price'],
'quantity' => $_POST['quantity']
);
$_SESSION['cart'][] = $session_array;
}
the picture above is just overwriting my selection everytime I press add to cart it won't stack up new item on top of the other. so basically the session array stays 0 all the time.
Pls somebody help me figure this out thank you so much!
When you do $_SESSION['cart'][0]= $session_array; you set it always on index 0. Try
array_push($_SESSION['cart'], $session_array);
Also this is not sticking $_SESSION['cart'][] = $session_array;
Always trust in array push.

decrease the stock when the transaction is completed codeigniter

i want to decrease stock product when transactions is complete, I try with foreach from data but the stock is not decreased correctly
CONTROLLER :
for ($i=0; $i < $count ; $i++) {
//SAVE DETAIL PENJUALAN
$data[] = array(
'nonota' => $this->input->post('nonota',TRUE),
'id_brg' => $this->input->post('kd_brg',TRUE)[$i],
'nama_brg' => $this->input->post('nama',TRUE)[$i],
'jml_brg' => $this->input->post('jml',TRUE)[$i],
'harga_brg' => $this->input->post('harga',TRUE)[$i],
);
//DELETE CART
$cart[] = array(
'rowid' => $this->input->post('rowid',TRUE)[$i],
'qty' => 0
);
$update[] = array(
'id' => $this->input->post('kd_brg',TRUE)[$i],
'stok' => 'stok' - $this->input->post('jml',TRUE)[$i]
);
$this->M_penjualan->updatestock($update,'tbl_barang');
}
MODEL
function updatestock($update) {
$this->db->update_batch('tbl_barang',$update,'id');
}
1.Maybe products ids are not the same as the ones you get from the iteration.
2.Take a look at this line:
'stok' => 'stok' - $this->input->post('jml',TRUE)[$i]
You change value of a string, because 'stok' is a string. It would be something else if you would make $stok - $this->input... .Of course if stock represents some number.
3.Also I would first try to update with simple update. $this->db->set('stok', 'stok-'.$stok, false); - somewhere in your model function.

How to update the quantity of all items in the cart with respect to database?

I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html

cannot add configurable product to cart in magento

i want to add a product to cart in magento , product details is :
type = configurable
product id = 1300
product sku = FCC100
Attribute Code= color
Attribute Label= color
Attribute Id= 152
options value = 28,43
options label = blue,red
this configurable product have two color option red and blue, there is two simple product atached to this configurable product.
i try with this code :
$product = array(
"product_id" =>"1300",
"qty" => 2,
"super_attribute" => array(152 => 28));
$result = $proxy->shoppingCartProductAdd($sessionID, $cartID, array($product));
but this code return me this message :
please specify product option(s)
i use this code to add simple product and it`s work fine :
$result = $proxy->shoppingCartProductAdd($sessionID, $cartID, array(array(
'product_id' => $productID ,
'sku' => $sku ,
'qty' => $qty,
'options' =>array(0 =>array('key' => $option1id ,'value' => $option1value),1 =>array('key' => $option2id ,'value' => $option2value)),
'bundle_option' => null,
'bundle_option_qty' => null,
'links' => null
)));
my problem is with configurable product. i try to add with simple child products but problem of this way is often child products have not price and price is setted to parent product.
what is the problem in my code ? is there any way to add configurable product to cart with out using API ?
this is what i found in product page source:
Product.Config({"attributes":{"152":{"id":"152","code":"color","label":"\u0631\u0646\u06af","options":[{"id":"28","label":"\u0622\u0628\u06cc","price":"0","oldPrice":"0","products":["1301"]},{"id":"47","label":"\u0632\u0631\u0634\u06a9\u06cc","price":"0","oldPrice":"0","products":["1302"]}]}},"template":"#{price}\u00a0\u0631\u06cc\u0627\u0644","basePrice":"550000","oldPrice":"550000","productId":"1300","chooseText":"\u0627\u0646\u062a\u062e\u0627\u0628 \u06cc\u06a9 \u06af\u0632\u06cc\u0646\u0647...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"\u0634\u0627\u0645\u0644 \u0645\u0627\u0644\u06cc\u0627\u062a"}});
Your array must be like below
$arrProducts = array(
array(
"product_id" =>"21",
"qty" => 2,
"super_attribute" => array(
92 => 162
)
));
Your $product array is missing the options key values.
You need to add options which should be An array in the form of option_id => content per the documentation.
$product = array(
"product_id" => "1300",
"qty" => 2,
"options" => array(
152 => 28
)
);
Documentation: http://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html

How do I count the number of each unique array inside a multidimentional array? PHP

I'd like to build a simple shopping cart using arrays. I need to display each unique item in the shopping cart aswell as the quantity of each item along side.
My initial cart array might look like this:
$cart=
array(
array(2003,100,"Table")
,array(2003,100,"Table")
,array(2003,100,"Table")
,array(2004,200,"Chair")
,array(2004,200,"Chair")
);
The first value in each array is the product_id, then the price & then the product name.
How do I print each unique item once aswell as the quantity of each along side?
Thanks in advance.
$new_cart = array();
foreach($cart as $product) {
if(!isset($new_cart[$product[0]])) {
$new_cart[$product[0]] = array('quantity' => 1, 'label' => $product[2], 'price' => $product[1]);
}
else {
$new_cart[$product[0]]['quantity']++;
}
}
I strongly suggest using associative arrays for this though. Your problem is the way you are storing the values. Try using something like this:
$cart_items = array(
2004 => array('quantity' => 3, 'label' => 'Leather Chair', 'price' => 200),
2901 => array('quantity' => 1, 'label' => 'Office Desk', 'price' => 1200),
);
When a user updates a quantity or adds an existing product simply increment the quantity.
You could simply iterate the array and use the product ID as key to count the amounts:
$amounts = array();
foreach ($cart as $item) {
if (!isset($amounts[$item[0]])) $amounts[$item[0]] = 0;
$amounts[$item[0]]++;
}
But it would be easier if your cart just stores the product IDs and amounts, so:
array(
2003 => 3,
2004 => 2
)
This is actually what the algorithm above is doing. But with this, you have all the information you need (product ID and amount).

Categories