Laravel 4 - Not updating sessions - php

I am trying to achieve the following:
I am building a very basic shopping cart, if someone adds an item to the basket, this gets saved inside a session. I.e. Session - array('id' => '1', 'quantity' => '1'); If they add another of these items with the same id, the product is not added again, but the quantity is adjusted with this.
My implementation:
$quantity = Session::get('basket_items2')[$product->id]['quantity'];
$newQuantity = (int)$quantity + 1;
$products[$product->id] = array("product_id" => $product->id, "quantity" => $newQuantity);
Session::put('basket_items2', $products);
However, the quantity just remains at 1 so no matter how many times you add the product, the quantity is always "2" therefore the session is not updating / overriding.
Any ideas, please?

Related

Display one attribute to group others around it in foreach loop PHP Laravel

In my project, all users are stored in the user table. The ones who are suppliers, their user key goes into the supplier table.
Each product can have multiple variations. The key product goes into the productattribute table.
Each supplier can have multiple products with multiple attributes.
So there is a many to many table of product attributes & suppliers named productsupplier.
I want to give the option to the user to make a new Purchase Order.
The Purchase Order will be for the supplier.
The user will select the supplier and then all the products that are assigned to him will show up.
The problem is that a product with the same name can have different attributes. Therefore, when I write the code for the products belonging to a supplier to show up, one product (which has different variations) shows up multiple times.
I have been struggling to use distinct or unique functions to show the name of one product once only, but since it's in a foreach loop, it doesn't work.
Here's the code:
public function new_po_next(Request $request)
{
$product = array();
$supplier = \App\supplier::where('user_id', $request->supplier)->first();
$productsupplier = \App\productsupplier::where('supplier_id', $supplier->id)->get();
foreach ($productsupplier as $ps) {
$productattribute = \App\productattribute::where('id', $ps->productattribute_id)->first();
$prod = \App\product::where('id', $productattribute->product_id)->first();
$product [] = [
'prod_supp_id' => $ps->id,
'prod_supp_prodattid' => $ps->productattribute_id,
'prod_supp_cost' => $ps->cost,
'prod_att_id' => $productattribute->id,
'prod_att_color' => $productattribute->color_id,
'prod_att_size' => $productattribute->size_id,
'prod_att_material' => $productattribute->material_id,
'prod_att_prodid' => $productattribute->product_id,
'prod_id'=>$prod->id,
'prod_name' =>$prod->name,
];
}
$status = $request->status;
$deldate = $request->deldate;
$discount = $request->discount;
return view("vendor.new-po-next")
->with('status', $status)
->with('deldate', $deldate)
->with('discount', $discount)
->with('product', $product);
}
This code is giving the following result (it's the correct result as product1 has 1 variation only but product5 has 3 variations).
What I want is that it shows 'product1' and 'product5' (product5 ONCE only).
If the user selects product5, then it shows the rest of the variations to select from.
Please let me know how it can be done!
How about using Laravel collections, you can convert your array to collection,
// After the loop is finished.
$productCollection = collect($product);
$groupedProductCollection = $collection->groupBy('prod_name');
$groupedProduct = $groupedProductCollection->all(); // all method will convert it back to array
// then return the value
return view("vendor.new-po-next")
->with('status', $status)
->with('deldate', $deldate)
->with('discount', $discount)
->with('product', $groupedProduct);
Working
Lets say your product array is like this
$product = [
['prod_name' => 'product5', 'prod_supp_cost' => '$4'],
['prod_name' => 'product5', 'prod_supp_cost' => '$3'],
['prod_name' => 'product1', 'prod_supp_cost' => '$6'],
]
Then after groupby it will be like,
[
'product5' => [
['prod_name' => 'product5', 'prod_supp_cost' => '$4'],
['prod_name' => 'product5', 'prod_supp_cost' => '$3'],
],
'product1' => [
['prod_name' => 'product1', 'prod_supp_cost' => '$6'],
],
]
Then you you show the array_keys in list and its values separately when the user clicks on it.
EDIT: How to use it in frontend
<select id="" name="">
#foreach(array_keys($groupedProduct) as $product_names)
<option value="{{ $product_names }}"></option>
#endforeach
</select>
If someone clicks then show it using $groupedProduct[$product_names]
Though the long term and best solution as suggested by #miken32 would be to use eloquent relationships & let relations handle all the querying so that there is no need for a for loop.

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

PHP Shopping Cart Array - how do i add sub arrays for multiple options for items in cart

I am working on a shopping cart where each product may have multiple options.
I believe the best way to achieve this is to have an array for my shopping cart containing the product id, then a sub array for each option/quantity the user chooses.
Before there were multiple options per product I used this...
$_SESSION['cart'][] = array(
"pid" => $_POST['pid'],
"quantity" => $_POST['quantity']
);
This worked fine, now I have multiple options per product how do I add the option and quantity as a 'sub array' - There may be up to 4 options/quantities added per product. I was hoping to do something like this...
$_SESSION['cart'][] = array(
"pid" => $_POST['pid'],
[] = array(
option => $_POST['option']
quantity => $_POST['quantity']
)
);
I know the above is not the right code, just an example to explain my gaol.
End result may be something like...
[1]-'product id'
-option1, Quantity1
-option2, Quantity1
[2]-'product id'
-option1, Quantity1
[3]-'product id'
-option1, Quantity1
-option2, Quantity1
-option3, Quantity1
How do I achieve this? Thanks in advance

php logic on a system shopping cart and session value management

this is a question about the logic of an e-commerce cart built in php:
i have an item in the cart saved with a session in php, this session is named by a composed string where the unique key is given by the product_id:
$this->session->data['quote_total_'.$product_id];
the problem is when i add the same product in the cart but with diefferent price - the price is generated automatically by the system at the change of misures, options, and quantity -
indeed the first value of the cart session that is the value of the first product price is overwritten by the new one. which is the best method to avoid this overwrite for you?
i was thinking about a unique value to append at the name of the session but i don't understand which ones...
How about making this variable an array instead of a primitive datatype?
Instead of using the product ID as a key, just let the key be created automatically which will be numerical and incremented with each product
// add a product - new product will always be added
$this->session->data[] = array(
'product_id' => 123,
'qty' => 1,
'name' => 'Product Name',
'price' => 10
);
// you can iterate over the products like
foreach($this->session->data as $product)
{
print_r($product);
}
Append price with product id in session variable to uniquely identify each product with different prices.And also use array to store multiple products.
Create multidimensional array such as
$data = array([0]=>array(
'product_id' => 123,
'qty' => 1,
'name' => 'Product Name',
'price' => 10),
[1]=>array(
'product_id' => 123,
'qty' => 2,
'name' => 'Product Name',
'price' => 20)
);

How do I set the quantity of the option in my bundle product?

I have this code, which would add one bundle product with the ID of 171 to the cart, given the option of a product with the color(23) Magenta(63). Is there any way to set a quantity to the option, like say we want to set the quantity to 2 of those items with that color?
Thanks for the help.
$params = array(
'product' => 171,
'bundle_option' => array(
23 => array(
0 => 63
),
),
'qty' => 1,
);
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->load(171);
$cart->addProduct($product, $params);
$cart->save();
Have you tried intercepting the request to addtocart and seeing what a normal post array for that bundle would look like? That should give you the structure that you need.

Categories