I'm trying to create a product label. While I create an array and pass it to the blade it's not working.
-> Controller
$item_name = $request->input('item_name');
$label_quantity = $request->input('label_quantity');
foreach ($item_name as $key => $name) {
$product = Product::find($item_name[$key]);
$barcode = Stock::where('product_id', '=', $item_name[$key])->get();
$data = [
'name' => $product->name,
'price' => $product->sale_price,
'barcode' => $barcode[0]['barcode'],
'quantity' => $label_quantity[$key],
];
return back()->with(compact('data'));
}
-> Blade File
#php
if (isset($data)) {
print_r(json_encode($data));
} else {
echo 'No Data Available';
}
#endphp
Please specify, what is inside the $item_name variable. Does it contain multiple products data?
If it contains only 1 product data then you change your code like this:
$item_name = $request->input('item_name');
$label_quantity = $request->input('label_quantity');
$product = Product::find($item_name[0]);
$barcode = Stock::where('product_id', '=', $item_name[0])->get();
$data = [
'name' => $product->name,
'price' => $product->sale_price,
'barcode' => $barcode[0]['barcode'],
'quantity' => $label_quantity[0],
];
return redirect('/abc', compact('data'));
Here you are redirecting to '/abc' route with $data array. You can also render a view and pass data to it like this:
return view('home', compact('data'));
return view('your view', ['data' => $data]);
if you want to pass many use compact built in function
return view('view name', compact(['data', 'data2', 'data3']));
-> Controller
$item_name = $request->input('item_name');
$label_quantity = $request->input('label_quantity');
foreach ($item_name as $key => $name) {
$product = Product::find($item_name[$key]);
$barcode = Stock::where('product_id', '=', $item_name[$key])->get();
$data = [
'name' => $product->name,
'price' => $product->sale_price,
'barcode' => $barcode[0]['barcode'],
'quantity' => $label_quantity[$key],
];
return back()->with(array('data'=>$data));
}
-> Blade File
#if (session('data'))
#php
print_r(json_encode(session('data')));
#endphp
#endif
Related
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'],
];
}
}
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();
}
I'm inserting an array of multiple inputs. but when i dd it or create it doesn't insert or returning any values. How can i use create in this situation? I'm new to laravel.
foreach ($data['sku'] as $key => $val) {
$attrCountSKU = ProductsAttribute::where('sku', $val)->count();
if ($attrCountSKU > 0) {
return back()->with('error', 'SKU already exists for this product! Please input another SKU.');
}
$attrCountSizes = ProductsAttribute::where(['product_id' => $product->id, 'size' => $data['size'][$key]])->count();
if ($attrCountSizes > 0) {
return back()->with('error', 'Size already exists for this product! Please input another Size.');
}
$attribute = new ProductsAttribute;
$attribute->product_id = $product->id;
$attribute->sku = $val;
$attribute->size = $data['size'][$key];
$attribute->price = $data['price'][$key];
$attribute->stock = $data['stock'][$key];
dd($attribute);
dd($attribute->create());
}
You need to save the model, using the save() method.
Add this after setting all the attributes:
$attribute->save();
return $attribute->id // Will be set as the object has been inserted
You could also use the create() method to create and insert the model in one go:
$attribute = ProductsAttribute::create([
'product_id' => $product->id,
'sku' => $val,
'size' => $data['size'][$key],
'price' => $data['price'][$key],
'stock' => $data['stock'][$key],
]);
Laravel Docs: https://laravel.com/docs/5.8/eloquent#inserting-and-updating-models
Instead of $attribute->create() you should use $attribute->save() method.
Or with the create() method you can do like this
$flight = ProductsAttribute::create(
[
'product_id' => $product->id,
'sku' => $val,
'size' => $data['size'][$key],
'price' => $data['price'][$key],
'stock' => $data['stock'][$key],
]
);
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);
}`
I used ArrayDataProvider to mix models and then merged them into one data-provider. I used the data-provider in grid-view and everything is works fine.
But I need to order the grid-view by one column I tried a lot of solutions but none of them are worked.
This my model code (order by item_order )
public function getAllSelectedItemsInTheUnit($unitId, $grid = false)
{
$finalList = array();
$storiesList = array();
$activityList = array();
$breakList = array();
$stories = UnitStories::find()->joinWith(['story'])->where("unit_id=$unitId")->all();
if (count($stories) > 0) {
foreach ($stories as $item) {
$storiesList[] = [
'key' => self::TYPE_STORY . $item->id,
'id' => $item->id,
'title' => $item->story->title,
'type' => self::TYPE_STORY,
'item_order' => $item->unit_order,
];
}
}
$activities = UnitActivities::find()->joinWith(['activity'])->where("unit_id=$unitId")->all();
if (count($activities) > 0) {
foreach ($activities as $item) {
$activityList[] = [
'key' => self::TYPE_ACTIVITY . $item->id,
'id' => $item->id,
'title' => $item->activity->title,
'type' => self::TYPE_ACTIVITY,
'item_order' => $item->activity_order,
];
}
}
$breaks = UnitBreaks::find()->where("unit_id=$unitId")->all();
if (count($breaks) > 0) {
foreach ($breaks as $item) {
$breakList[] = [
'key' => self::TYPE_BREAK . $item->id,
'id' => $item->id,
'title' => $item->title,
'type' => self::TYPE_BREAK,
'item_order' => $item->unit_order,
];
}
}
$finalList = array_merge($storiesList, $activityList, $breakList);
$dataProvider = new ArrayDataProvider([
'allModels' => $finalList, 'key' => 'key',
'sort' => [
'attributes' => ['item_order'],
],
]);
return $dataProvider;
}
Any solution will be very good even sort array by pure PHP I guess will fix the problem .
You can use usort()
usort($finalList, function ($a, $b) {
return $a['item_order'] < $b['item_order'];
});
Add your condition in callback >, <, <= etc