2014-01-14 4 views
1

Я пытаюсь экспортировать заказы элемент из плагина в WordPress Woocommerce и создать шаблон в WordPress с моим querys для получения данных заказов внутри массиваЭкспорт заказов из WooCommerce в JSON ФОРМАТЕ

<?php 
//query 
     global $woocommerce; 
     global $wpdb; 
     global $product; 
     $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); 
     $order_id = $loop->post->ID; 
     $order = new WC_Order($order_id); 
     $product = new WC_Product($order_id); 

    //Email verificacion if is suscribed or not 
    $sql = "SELECT * FROM dsr_wysija_user WHERE email='". $order->order_custom_fields['_billing_email'][0] ."'" ; 
    $res = mysql_query($sql); 
    if (mysql_num_rows($res)== 1){ 
    $email = 'true'; 
    }else{ 
    $email = 'false'; 
} 
// Getting names of categories and quantity 
foreach($order->get_items() as $item) 
{ 
    $item['name']; 
    $item['qty']; 
} 


//Collecting data in 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"=>''. $order->order_custom_fields['_billing_country'][0] .'', 
     "terms_accepted"=>'true', 
     "receiving_mails_accepted"=>''.$email.'', 
     "email"=>''. $order->order_custom_fields['_billing_email'][0] .'', 
     "original_created_at"=>''.$order->order_date.'',); 


    //Starting While 
    while ($loop->have_posts()) : $loop->the_post(); 
    //Printing Output array 
    // echo (json_format($json)) 
?> 


<?php endwhile; ?> 

Ouput подобна той

{ 
    "salutation": "Arq.", 
    "title": "herr", 
    "first_name": "Ted", 
    "last_name": "Mosby", 
    "street": "Manhattan", 
    "street_number": "20", 
    "address_supplement": "How I met your mother", 
    "zipcode": "MANHATTAN", 
    "city": "New York", 
    "country": "ES", 
    "terms_accepted": "true", 
    "receiving_mails_accepted": "true", 
    "email": "[email protected]", 
    "original_created_at": "2014-01-07 03:34:31" 
}  

Но что мне нужно, это

{ 
    "salutation": "Arq.", 
    "title": "herr", 
    "first_name": "Ted", 
    "last_name": "Mosby", 
    "street": "Manhattan", 
    "street_number": "20", 
    "address_supplement": "How I met your mother", 
    "zipcode": "MANHATTAN", 
    "city": "New York", 
    "country": "ES", 
    "terms_accepted": "true", 
    "receiving_mails_accepted": "true", 
    "email": "[email protected]", 
    "original_created_at": "2014-01-07 03:34:31" 
    "items": [ 
    { 
     "campaign_number": 301, 
     "item_number": 1 
    }, 
    { 
     "campaign_number": 301, 
     "item_number": 2 
    }, 
    ... 
    ] 
}, 
... 
] 
} 
+0

Какие вещи? –

+0

Товары это товары, например Пункт «Лето в Берлине» и т. Д. – Deimos

ответ

1

Предполагая это ваш товар петля

// Getting names of categories and quantity 
foreach($order->get_items() as $item) 
{ 
    $json['items'][] = array (
     'name' => $item['name'], 
     'quantity' => $item['qty'] 
} 

Try выше кода непосредственно перед while ($loop->have_posts())

+0

Большое спасибо Рахиль Вазир !!!! Ты лучший! хехе;) – Deimos

Смежные вопросы