我试图完成这个功能,(WORDPRESS WOOCOMMERCE ORDERS出口到JSON格式并发送邮件)我现在得到了我的JSON数据,但不是我需要的。我得到了JSON IN 2 LINES [JSON DATA ....] [JSON DATA2 ...]但我需要这样的[JSON DATA,JSON DATA2,JSON DATA3 ....]
我如何生成此输出...
查询和代码
<?php
//query
global $woocommerce;
global $wpdb;
$args = array(
'post_type' => 'shop_order',
'orderby' => 'ID',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('processing'))));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items(); ?>
$ json = array(.... contents)就在这里
<?php
//Colecting all the datas in array
// All the information of the customer
$json_full = array();
$json=array(
"salutation"=>''. $order->order_custom_fields['_billing_titel'][0] .'' ,
"title"=>''. $order->order_custom_fields['_billing_anrede'][0] .'',
"first_name"=>''. $order->order_custom_fields['_billing_first_name'][0] .'',
"last_name"=>''. $order->order_custom_fields['_billing_last_name'][0] .'',
"street"=>''. $order->order_custom_fields['_billing_address_1'][0] .'',
"street_number"=>''.$order->order_custom_fields['_billing_address_2'][0] .'',
"address_supplement"=>''. $order->order_custom_fields['_billing_company'][0] .'',
"zipcode"=>''. $order->order_custom_fields['_billing_postcode'][0] .'',
"city"=>''. $order->order_custom_fields['_billing_city'][0] .'',
"country"=>''.$pais.'',
"terms_accepted"=>true,
"receiving_mails_accepted"=>$email,
"email"=>''. $order->order_custom_fields['_billing_email'][0] .'',
"lottery_accepted"=> false,
"lottery_solution"=> "LOTTERY",
"original_created_at"=>''.date( 'Y-m-d\TH:i:s', strtotime( $order->order_date ) ).'',
);
// $json = array_map('htmlentities',$json);
//$json = html_entity_decode(json_format($json));
//Getting order name and quantity
// $productmeta = new WC_Product($id);
//$sku = $productmeta->post->sku;
foreach($items as $item)
{
$sku = $wpdb->get_var( $wpdb->prepare('SELECT meta_value FROM dsr_postmeta WHERE meta_key="_sku" AND post_id='.$item['product_id'].''));
$sku_s = explode('-', $sku);
$camp = $sku_s[1];
$itm_n = $sku_s[0];
$json['items'][] = array (
"campaign_number"=>(int)$camp,
"item_number" =>(int)$itm_n, // Here appear the name of the product
//'item_number' => $item['qty']); // here the quantity of the product
);}array_push($json_full, $json);?>
我的产出
[{"salutation":"Prof","title":"herr","first_name":"Michael","last_name":"Schumacher","street":"Insekammer","street_number":"2","address_supplement":"Media","zipcode":"81569","city":"Munich","country":"Deutschland","terms_accepted":true,"receiving_mails_accepted":true,"email":"schuma@cher.com","lottery_accepted":false,"lottery_solution":"LOTTERY","original_created_at":"2014-01-16T23:13:12","items":[{"campaign_number":66,"item_number":481}]}]
[{"salutation":"Dr.","title":"herr","first_name":"Julia","last_name":"G\u00f3mez","street":"c\/ luis cernudatte","street_number":"1","address_supplement":"Internauticos","zipcode":"SALTERAS","city":"Sevilla","country":"Spanien","terms_accepted":true,"receiving_mails_accepted":true,"email":"mein@domain.com","lottery_accepted":false,"lottery_solution":"LOTTERY","original_created_at":"2014-01-07T12:42:50","items":[{"campaign_number":66,"item_number":481}]}]
但是,我真正需要的是
Like this example
[
{
"salutation": "Herr",
"title": "Prof.",
"first_name": "Max",
"last_name": "Schulz",
"street": "Geranienweg",
"street_number": "11",
"address_supplement": "Robinienstr. 23",
"zipcode": "47509",
"city": "Berlin",
"country": "Deutschland",
"terms_accepted": true,
"receiving_mails_accepted": false,
"email": "example@domain.com",
"lottery_accepted": false,
"lottery_solution": "LOTTERY",
"original_created_at": "2013-10-30T18:10:28+01:00",
"items": [
{
"campaign_number": 393,
"item_number": 2
},
{
"campaign_number": 394,
"item_number": 5
}
]
}, <---! here continuation of another order.
{
"salutation": "Herr",
"first_name": "Frank",
"street_number": "11",
"zipcode": "47509",
"city": "Berlin",
"country": "Deutschland",
"terms_accepted": true,
"items": [
{
"campaign_number": 393,
"item_number": 2
}
]
}
]<- here the end of the ALL orders.
我的邮件发送到服务器的代码,我不知道是否正确,但服务器的响应是
<?php
$url = "url";
$content = json_encode($json_full);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode ?>
在我的输出中我得到了块状,我认为这是因为循环或wordpress时我会感谢你的帮助
答案 0 :(得分:0)
创建一个新变量$ json = array()并为每个值设置$ json [] = array([values])。 不要使用array_push。