how to store simply multi array in session laravel 5.6 - php

i store one array in to session('cart') last click [add to cart] , when i add another array in session('cart'), but it store one array then session can not save both.
help me !
public function addtocart(Request $req,$id){
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$req->session()->put('cart',$cart);
$a = session()->get('cart');
}

change this
$req->session()->put('cart',$cart);
$a = session()->get('cart');
dd($a);
to this:
$cartvalues[] = $cart;
$req->session()->put('cart',$cartvalues);
$a = session('cart');
dd($a);
because you keep overwriting the previous value inside the session cart

Here you are overwriting cart variable in session every time you add a new item. So store cart items as an array and add item to array. Code should be:
public function addtocart(Request $req, $id) {
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$cartItems = session()->get('cart');
if (empty($cartItems)) {
$cartItems = [];
}
$cartItems[] = $cart;
$req->session()->put('cart', $cartItems);
return view($cartItems);
}

Here is the simple example to update your cart in your case
`
public function addtocart(Request $req,$id){
//return redirect('Resouce/product');
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
if($req->session()->has('cart')){
$oldCart = $req->session()->get('cart');
$newCart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => array_merge($item,$oldCart['item'])
];
$req->session()->put('cart',$newCart);
}
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$req->session()->put('cart',$cart);
$a = session()->get('cart');
dd($a);
}`

Related

array_push() not working with session in PHP

i am working with PHP, I want to store more than two products in SESSION using array_push(). But problem is that after array_push only 2 products is showing in the cart. When i add more than two product then it is not added into the cart.
Here is my Code:
$dataArray = array();
$cartArray = array(
$code=>array(
'id' => $id,
'name' =>$name,
'price' =>$price,
'quantity' =>1)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
}
else {
array_push($dataArray, $_SESSION["shopping_cart"], $cartArray);
$_SESSION['shopping_cart'] = $dataArray;
}
You can directly assign values to an array like the below mention.
$_SESSION['shopping_cart'][] = $dataArray;
It will create a 2-d array for "shopping_cart" and every time you add $dataArray
it will store in new key so you can get the "shopping_cart" array having all items
For more about array go throgh this :- php arrays
Please find solution below:
<php
$cartArray = [
[
'id' => $id,
'name' =>$name,
'price' =>$price,
'quantity' =>1
],
[
'id' => $id,
'name' =>$name,
'price' =>$price,
'quantity' =>1
]
];
if(isset($_SESSION["shopping_cart"])){
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
}
else {
array_push($_SESSION["shopping_cart"], $cartArray);
}
}
?>

how to condition same time product to insert and update in laravel

I am tring Order update.I need to update multi product in different row. when I update the product then it only updated created fields. i want to update current row and then new product insert in different row.
$product_id = $request->product_id;
$seller_id = Auth::user()->id;
$price = $request->product_price;
$quantity = $request->product_qty;
$payment_status = $request->payment_status;
$delivery_status = $request->delivery_status;
$shipping_type = "home_delivery";
$shipping_charge = $request->shipping_cost;
$user = Address::all()->where('user_id', $request->user_id)->first();
$cname= DB::table('users')->where('id', $user->user_id)->value('name');
$shipping_address = [
'user_id'=>$request->user_id,
'name'=> $request->name,
'phone'=>$request->phone,
'email'=>$user->email,
'city'=>$user->city,
'postal_code'=>$user->postal_code,
'country'=>$user->country,
'address'=>$request->address,
];
$order_id=$request->id;
$order = Order::findOrFail($order_id)->update([
'seller_id' => Auth::user()->id,
'shipping_address' => json_encode($shipping_address),
'payment_status' => $request->payment_status,
'payment_type' => $request->payment_option,
'payment_details' => $request->payment_option,
'coupon_discount' => $request->coupon_discount,
'round_amount' => $request->round_discount,
'grand_total' => $request->grand_total,
]);
for($i = 0; $i <= count($request->product_id)-1; $i++){
$data = [
'order_id' => $order_id,
'product_id' => $product_id[$i],
'seller_id' => $seller_id,
'price' => $price[$i],
'quantity' => $quantity[$i],
'payment_status' => $payment_status,
'delivery_status' => $delivery_status,
'shipping_type' => $shipping_type,
'shipping_cost' => $shipping_charge,
];
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
}
else{
Orderdetail::insert($data);
}
$product_stock_info = ProductStock::where('product_id', $product_id[$i])->first();
// dd($product_id[$i]);
$stock = $product_stock_info->qty;
$updateStock = $stock - $quantity[$i];
$product_stock_info->qty = $updateStock;
$product_stock_info->save();
}
my problem is here
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
} else {
Orderdetail::insert($data);
}
You can use updateOrCreate method .
the updateOrCreate method persists the model, so there's no need to manually call the save method
$flight = Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
as like
$whereData = [
'order_id' => $order_id
];
OrderDetail::updateOrCreate($whereData ,$data);

Send payment gateway woocommerce producct description next to product name

I want to send the
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ($items as $item) {
$data = $item->get_data();
$arr[] = [
'productSKU' => $data['product_id'],
'description' => $data['name'],
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}
Here in this part:
'description' => $data['name'],
I want it to send product description next to product name as example below:
Product Name - Product Description
How can achieve this ?
Best Regards
If you need product description saved in $content, you can get this by Product_ID, and then add this to your array:
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ( $items as $item ) {
$data = $item->get_data();
$product_description = get_post( $data['product_id'] )->post_content;
$arr[] = [
'productSKU' => $data['product_id'],
'name' => $data['name'],
'description' => $product_description,
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}
Or if you need built-in woocommerce product description, try this:
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ( $items as $item ) {
$data = $item->get_data();
$product_instance = wc_get_product( $data['product_id'] );
$product_full_description = $product_instance->get_description();
$product_short_description = $product_instance->get_short_description();
$arr[] = [
'productSKU' => $data['product_id'],
'name' => $data['name'], // or $data['description']
'description' => $product_full_description, // or $product_short_description
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}

How to update item in session array in laravel

I need update 1 item of my session , i send a new quantity from my form, $item['quantity'] change but not Session::get('details_evacuation_file')
It's the structure
Session::push('details_evacuation_file', [
'id' => $p_temp->id,
'quantity' => 1,
'cost' => null,
'discount' => 0,
'product' => $p_temp
]);
My function
public function update_quantity(Request $request) {
$details = Session::get('details_evacuation_file', []);
foreach ($details as $item) {
if ($item['id'] == $request->input('id')) {
var_dump($item['quantity']);
$item['quantity'] = $request->input('quantity');
var_dump($request->input('quantity'));
var_dump($item['quantity']);
}
}
session('details_evacuation_file', $details );
var_dump(Session::get('details_evacuation_file'));
die();
return redirect()->back();
}

Woocommerce rest api update product image gives error

I want to update my product with product image but it is not updated. I am using woocommerce rest API for adding a product.
I am following these step for update product:
Create a view file for display all the product list
Create edit function in the controller for getting all product data
when uploading a new image from edit form it gives an error
Here is my code:
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move('C:/xampp/htdocs/New-flex/wp-content/uploads/backend-product-image', $name);
$productimg_path_store_db = "http://localhost/New-flex/wp-content/uploads/backend-product-image/";
$productimg_allimg[] = $productimg_path_store_db.$name;
}
}
$AddContentText = $request->get('AddContentText');
if (!empty($AddContentText)) {
/* For List All product qnumber check in array */
$newallproducts_data = $woocommerce->get('products',array('per_page' => 100));
$array = json_decode(json_encode($newallproducts_data), True);
$partssku = array();
$partid = array();
$parturl = array();
foreach ($array as $key ) {
$partssku[] = $key['sku'];
$partid[] = $key['id'];
$parturl[] = $key['permalink'];
}
/* Create associative array product id and sku for compare */
$comb = array_combine($partssku,$partid);
/* Combine content QNUMBER and QTY in array */
$chunks = array_chunk(preg_split('/(;|,)/', $AddContentText), 2);
$result = array_combine(array_column($chunks, 0), array_column($chunks, 1));
/*Product qnumber allready avilabale array*/
$diff = array_intersect_key($result,$comb);
/*NEW PRODUCT array*/
$newproduct = array_diff($result, $diff);
/* Qnumber insert in database */
$parts_name = implode(',', array_keys($result));
/* count insert in database */
$noofpartsuse = implode(',', array_values($result));
/*echo "<pre>";
echo "main data";
print_r($result);
echo "allproduct array";
print_r($comb);
echo "Product qnumber allready avilabale";
print_r($diff);
echo "NEW PRODUCT";
print_r($newproduct);
echo "</pre>";*/
// print_r($newproduct_data);
//print_r($woocommerce->post('products', $data));
}
else{
$parts_name = '';
$noofpartsuse = '';
}
$productactive = $request->get('productactive');
if(!empty($productactive))
{
$productactive = "publish";
}
else{
$productactive = "draft";
}
$ProductListOrder = $request->get('ProductListOrder');
$producttype = $request->get('producttype');
$ProductQNumber = $request->get('ProductQNumber');
$ProductName = $request->get('name');
$ProductWidthMM = $request->get('ProductWidthMM');
$ProductLengthMM = $request->get('ProductLengthMM');
$ProductWidthInch = $request->get('ProductWidthInch');
$ProductLengthInch = $request->get('ProductLengthInch');
$Productinfotext = $request->get('Productinfotext');
$Producttechnocaldesc = $request->get('Producttechnocaldesc');
$metadescription = $request->get('metadescription');
$proimgalt = $request->get('proimg-alt');
$proyoutubelink = $request->get('proyoutubelink');
$proyoutubelinkalt = $request->get('proyoutubelink-alt');
$provimeolink = $request->get('provimeolink');
$provimeolinkalt = $request->get('provimeolink-alt');
$onshapelink = $request->get('onshapelink');
$onshapelinkalt = $request->get('onshapelink-alt');
$data = [
'name' => $ProductName,
'type' => $producttype,
'status' => $productactive,
'regular_price' => '',
'description' => $Productinfotext,
'short_description' => $Producttechnocaldesc,
'sku' => $ProductQNumber,
'categories' =>array (),
'meta_data' => [
[
'key' => 'list_order',
'value' => $ProductListOrder
],
[
'key' => 'ProductWidthMM',
'value' => $ProductWidthMM
],
[
'key' => 'ProductLengthMM',
'value' => $ProductLengthMM
],
[
'key' => 'ProductWidthInch',
'value' => $ProductWidthInch
],
[
'key' => 'ProductLengthInch',
'value' => $ProductLengthInch
],
[
'key' => 'proyoutubelink',
'value' => $proyoutubelink
],
[
'key' => 'proyoutubelink-alt',
'value' => $proyoutubelinkalt
],
[
'key' => 'provimeolink',
'value' => $provimeolink
],
[
'key' => 'provimeolink-alt',
'value' => $provimeolinkalt
],
[
'key' => 'onshapelink',
'value' => $onshapelink
],
[
'key' => 'onshapelink-alt',
'value' => $onshapelinkalt
],
[
'key' => 'UseParts-link',
'value' => $parts_name
],
[
'key' => 'Noofparts-use',
'value' => $noofpartsuse
]
],
'images' => array ()
];
/* image array for store image */
if(!empty($productimg_allimg)){
$images = &$data['images'];
$n = 0;
foreach($productimg_allimg as $id)
{
$images[] = array( //this array must be created dynamic
'src' => $id,
'position' => $n++
);
}
unset($images);
}
if(!empty($_POST['categories'])){
/* Producta category array */
$categories = &$data['categories'];
foreach($_POST['categories'] as $cat)
{
$categories[] = array( //this array must be created dynamic
'id' => $cat
);
}
unset($categories);
}
$data_insert = $woocommerce->post('products', $data);
if($data_insert){
foreach ($newproduct as $key => $value) {
$newproduct_data = [
'name' => $key,
'sku' => $key
];
$woocommerce->post('products', $newproduct_data);
}
}
return redirect('products')->with('success', 'Product has been Added');
Above code written in laravel

Categories