I create an eCommerce store with laravel 9.
I install stripe API, but I have some difficulty displaying product data in the stripe session.
Here is the function:
public function stripe()
{
\Stripe\Stripe::setApiKey(config(key:'stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'EUR',
'product_data' => [
'name' => 'huile',
],
'unit_amount' => '30',
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route(name:'shop'),
'cancel_url' => route(name:'shop'),
]);
return redirect()->away($session->url);
}
I tried to create function type array return product_ price and product_name from Cart content.but always have issues. So if there are some ideas to how can i display data from the product cart to the stripe session.
I want to call product data from Cart collection. and display it in stripe session.
I tried to create function return array
public function getproducts()
{
$cart = Cart::content()->load('product');
$cartArray = $cart->toArray();
return ['cart' => $cartArray];
}
Related
I want to set a quantity for my items in darryldecode/laravelshoppingcart but it doesn't set and when I get dd there is no quantity in item. It's my cart controller:
public function add(Request $request)
{
$product = Product::findOrFail($request->product_id);
$rowId = $product->id;
Cart::add(array(
'id' => $rowId,
'name' => $product->name,
'price' => $product->is_sale ? $product->sale_price : $product->price,
'quantity' => 1,
'attributes' => $product->toArray(),
'associatedModel' => $product
));
return redirect()->back();
}
I'm using laravel 8 and I updated the package to latest version
How can I use guards to add a specific admin menu entry using guards? I know I can pass guard-"data" from controllers to view like in the docs mentioned:
class UsersController extends Controller
{
public function index()
{
return Inertia::render('Users/Index', [
'can' => [
'create_user' => Auth::user()->can('users.create'),
],
'users' => User::all()->map(function ($user) {
return [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
//THIS BELOW IS THE GUARD-DATA
'can' => [
'edit_user' => Auth::user()->can('users.edit', $user),
]
];
}),
]);
}
}
The question here is where is the controller for the AppLayout.vue file so I can accomplish this?
In this case i'll suggest you to share guards/permissions through HandleInertiaRequests.php file. I have used spatie/permissions to manage middlewares.
public function share(Request $request)
{
return array_merge(parent::share($request), [
'auth' => [
'user' => [
'id' => $request->user()->id ?? '',
'name' => $request->user()->name ?? '',
'permissions' => $request->user() ?
$request->user()->getPermissionNames() : '',
],
]
}
You can easilly access this data in vue template as $page.props.auth.user.permissions.
<template>
<div v-if="can('edit-post')">
//Edit Post
<div>
<template>
<script>
methods:{
can(permission){
// Check Permissions
let data = this.$page.props.auth.user.permissions
.filter((ability) => ability === permission);
return data.length > 0 ? true : false;
}
}
<script>
I am adding order to database. It works like this: When ordering is clicked, the order is created in the Order table, at the same time the product items are also added to the OrderItem table, via the order_id foreign key. But I don't know how to get the order_id, because it is added at the same time, and the Order id is increments.
public function save(array $data, int $id = null){
$idCurrent = Auth::id();
$orderItems = $data['orderItems'];
//add to Order
Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
//add to OrderItem
foreach($orderItems as $item){
OrderItem::Create([
'order_id' => 222, //=> ?????????????
'product_id' => $item -> product_id,
'quantity' => $item->quantity,
]);
}
return true;
}
$order = Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
//add to OrderItem
foreach($orderItems as $item){
OrderItem::Create([
'order_id' =>$order->id
'product_id' => $item -> product_id,
'quantity' => $item->quantity,
]);
}
return true;
Avoid multiple call to databases.
When you create order with
$order = Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
next you should is to send just one request to database with
// create batch array
$insertOrderItems = [];
foreach($orderItems as $item){
$insertOrderItems[] = [
'product_id' => $item->product_id,
'quantity' => $item->quantity,
];
}
// insert all at once in batch mode making just one call to database
$order->orderItems()->create($insertOrderItems);
Presuming you have sorted relations in Order and OrderItem models.
I have 4 extra attributes ('product_id', 'quantity', 'discount_percent', 'discount_amount') in my pivot table, but my values for these are always 0 when I store, while the rest is populated properly. Any ideas?
Invoice model
public function productversion()
{
return $this->belongsToMany('App\Productversion')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}
Productversion model
public function invoice()
{
return $this->belongsToMany('App\Invoice')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}
Controller (store)
$invoice->productversion()->attach($productversionid, ['product_id' => $productid], ['quantity' => $qty], ['discount_percent' => $discountprc], ['discount_amount' => $discountamt]);
Just one array will do:
$invoice->productversion()->attach($productversionid, [
'product_id' => $productid,
'quantity' => $qty,
'discount_percent' => $discountprc,
'discount_amount' => $discountamt
]);
In addition to #jeff's method, you can attach or sync multiple elements with pivot attributes providing a multidimensional array that has ids as keys.
$attach_data[$productversionid] = [
'product_id' => $productid,
'quantity' => $qty,
'discount_percent' => $discountprc,
'discount_amount' => $discountamt
];
Then you can $invoice->productversion()->attach($attach_data).
Substantially you can pass an array like
[
'relation_id' => [
// pivot data
],
'relation_id' => [
// pivot data
],
// ...
]
I am trying to integrate laravels moltin cart but anytime I add a new item it replaces the previous item in the cart. Hence i'm not able to add more than one item to the cart.
Below is the route that handles the add to cart request and the associated controller.
Route:
Route::get('/cartadd/{pid}', 'CartController#add_to_cart');
Controller:
public function add_to_cart($pid)
{
$product = Product::find($pid);
Cart::insert(array(
'id' => $pid,
'name' => $product->name,
'price' => $product->price,
'quantity' => 1,
'tax' => 0,
'seller' => $product->business_id
));
$cart = Cart::contents();
return view('site.cart', array(
'cart' => $cart,
'page_title' => 'Your shopping cart',
'description' => '',
'page' => 'home'
));
}
The solution i found was to change the storage from session to cache in the config/moltincart file.
This file is created after running php artisan vendor:publish in command line.