Reputation: 7128
I need to loop my cart data and get some part of it like: title & price
for my 3rd party online payment.
Here is what I have:
$items = Cart::getContent();
foreach($items as $item)
{
$item->id; // the Id of the item
$item->name; // the name
$item->price; // the single price without conditions applied
$item->getPriceSumWithConditions(); // the subtotal with conditions applied
$item->quantity; // the quantity
}
$items = [
array(
'id' =>
'price' =>
'quantity' =>
'name' =>
)
];
My question is:
How should I fill my arrays? I mean should I make something like:
$id = $item->id;
in my foreach and then say 'id' => $id
or just say 'id' => $item->id
in my array?
PS: I know this question might seem silly question to some of you but as I cannot
dd
my results that's why i asked here,why?
because my data goes to 3rd party website by api and all i get is not getting result. so really can't dd it in my front-end. that's why i asked here.
I've changed my function like:
$items = Cart::getContent();
foreach($items as $item)
{
array(
'id' => $item->id,
'price' => $item->getPriceWithConditions(),
'quantity' => $item->quantity,
'name' => $item->name,
);
}
Issue: is that I still can't get my popup form as you see in my sample video above.
Here is my complete code:
public function payonline(){
// midtrans
error_log('masuk ke snap token dri ajax');
$midtrans = new Midtrans;
$transaction_details = array(
'order_id' => uniqid(),
'gross_amount' => $request->input('totalPriceInTotal')
);
$items = Cart::getContent();
foreach($items as $item)
{
array(
'id' => $item->id,
'price' => $item->getPriceWithConditions(),
'quantity' => $item->quantity,
'name' => $item->name,
);
}
$orderaddress = $request->input('address_id');
// Populate customer's shipping address
$shipping_address = array(
'first_name' => $request->input('buyer_name'),
'last_name' => $request->input('buyer_name'),
'address' => $orderaddress->address,
'city' => $orderaddress->city->name,
'postal_code' => $orderaddress->postalcode,
'phone' => $request->input('phone'),
'country_code' => 'IDN'
);
// Populate customer's Info
$customer_details = array(
'first_name' => $request->input('buyer_name'),
'last_name' => $request->input('buyer_name'),
'email' => $request->input('buyer_email'),
'phone' => $request->input('phone'),
'billing_address' => $shipping_address,
'shipping_address'=> $shipping_address
);
// Data yang akan dikirim untuk request redirect_url.
$credit_card['secure'] = true;
//ser save_card true to enable oneclick or 2click
//$credit_card['save_card'] = true;
$time = time();
$custom_expiry = array(
'start_time' => date("Y-m-d H:i:s O",$time),
'unit' => 'hour',
'duration' => 2
);
$transaction_data = array(
'transaction_details'=> $transaction_details,
'item_details' => $items,
'customer_details' => $customer_details,
'credit_card' => $credit_card,
'expiry' => $custom_expiry
);
try
{
$snap_token = $midtrans->getSnapToken($transaction_data);
//return redirect($vtweb_url);
echo $snap_token;
}
catch (Exception $e)
{
return $e->getMessage;
}
}
Upvotes: 0
Views: 263
Reputation: 676
Assumming that all of the formats you mentioned in your code is correct to the 'Midtrans' API specification, then all you need is to assign an array variable to your foreach loop of your items like this:
$items = Cart::getContent();
$item_details = array();
foreach($items as $item)
{
$item_details[] = array(
'id' => $item->id,
'price' => $item->getPriceWithConditions(),
'quantity' => $item->quantity,
'name' => $item->name,
);
}
Then replace your transaction data with the formatted array $item_details
$transaction_data = array(
'transaction_details'=> $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
'credit_card' => $credit_card,
'expiry' => $custom_expiry
);
Upvotes: 1
Reputation: 2993
something like this..
$items = Cart::getContent();
$yourItems = [];
foreach($items as $item)
{
$data = ['id' => '','price'=> '', 'quantity' => '', 'name' =>'', ];
$data = ['id'] = $item->id; // the Id of the item
$data = ['name'] = $item->name; // the name
$data = ['price'] = $item->price; // the single price without conditions applied
$data = ['quantity'] = $item->quantity; // the quantity
array_push($yourItems, $data);
}
Upvotes: 0