我需要循环购物车数据并获取部分内容like: title & price
以进行第三方在线支付。
这就是我所拥有的:
$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' =>
)
];
我的问题是:
我应该如何填充数组?我的意思是我应该做点什么:
在我的foreach中 $id = $item->id;
,然后在我的数组中说'id' => $id
或只说'id' => $item->id
?
PS:我知道这个问题对你们中的一些人来说可能看起来很愚蠢,但我不能dd
我的结果是我在这里问why?
的原因,因为我的数据是通过第3方网站进行的 api和我得到的都没有得到结果。所以真的可以在我的脑海中找到它 前端。这就是我在这里问的原因。
我改变了我的功能:
$items = Cart::getContent();
foreach($items as $item)
{
array(
'id' => $item->id,
'price' => $item->getPriceWithConditions(),
'quantity' => $item->quantity,
'name' => $item->name,
);
}
问题:是我仍然无法获得我在上面的示例视频中看到的弹出窗体。
这是我的完整代码:
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;
}
}
答案 0 :(得分:1)
假设您在代码中提到的所有格式都符合“Midtrans”API规范,那么您只需要为项目的foreach循环分配一个数组变量,如下所示:
$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,
);
}
然后用格式化的数组$ item_details
替换您的交易数据$transaction_data = array(
'transaction_details'=> $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
'credit_card' => $credit_card,
'expiry' => $custom_expiry
);
答案 1 :(得分:0)
$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);
}