以WooCommerce API文档中的以下代码为例。我想做的是在数组中添加条件。例如,基于某些if条件,我希望payment_details
数组成为$data
的一部分。这可能吗?怎么样?
<?php
$data = [
'order' => [
'payment_details' => [
'method_id' => 'bacs',
'method_title' => 'Direct Bank Transfer',
'paid' => true
],
'billing_address' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe@example.com',
'phone' => '(555) 555-5555'
],
'shipping_address' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'customer_id' => 2,
'line_items' => [
[
'product_id' => 546,
'quantity' => 2
],
[
'product_id' => 613,
'quantity' => 1,
'variations' => [
'pa_color' => 'Black'
]
]
],
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
]
]
];
print_r($woocommerce->post('orders', $data));
?>
重点是,我不想在这里放置if条件,而不是再次定义整个数组:
'order'=> [
如果($ payment ='xyz'){
'payment_details' => [
'method_id' => 'bacs',
'method_title' => 'Direct Bank Transfer',
'paid' => true
],
} 其他{
'payment_details' => [
'method_id' => 'monopoly',
'method_title' => 'Monopoly',
'paid' => true
],
}
是否可以使用点等于连接数组? 。= 谢谢。
答案 0 :(得分:1)
“ payment_details”已经是$ data的一部分。
要使用它,请使用:
$paymentDetails = $data['order']['payment_details'];
然后您可以通过以下方式显示它:
echo $paymentDetails['method_title'];
如果要使用if条件:
if ($paymentDetails['method_title'] === 'Monopoly money') {
echo 'That will not work';
}
要更改付款明细,请执行以下操作:
$data['order']['payment_details']['method_title'] = 'Changed';
或
$data['order']['payment_details'] = ['method_title' => 'something', 'method_id' => 5, 'paid' => false];
答案 1 :(得分:1)
您可以预先定义payment_details,并在$ data数组中使用它。
dar:27 january: case+receive -> 0
dar:27 february: case+receive -> 0
dar:27 june: case+receive -> 0
dar:27 april: case+receive -> 0
dar:27 march: case+receive -> 0
dar:27 may: case+receive -> 0
dar:27 july: case+receive -> 0
dar:27 august: case+receive -> 0
dar:27 september: case+receive -> 0
dar:27 october: case+receive -> 0
dar:27 november: case+receive -> 0
dar:27 december: case+receive -> 0
答案 2 :(得分:0)
回答我自己的问题。这就是我想要的。
$orderData = [];
if ($payment == 'Monopoly') {
$orderData['order']['payment_details'] = [];
$orderData['order']['payment_details']['method_id'] = 'Monopoly';
$orderData['order']['payment_details']['method_title'] = 'Monopoly';
$orderData['order']['payment_details']['paid'] = true;
$orderData['order']['status'] = 'completed';
}
else {
$orderData['order']['status'] = 'pending';
}