2014-01-14 3 views
2

Я пытаюсь получить sku из заказов Woocoomerce (плагин из электронной коммерции Wordpress) Мне нужно указать имя заказа продукта (но это у меня сейчас, только единицы складского хранения является проблемой) Это мой код,Получить sku с каждого заказа Woocommerce

<?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); 

     while ($loop->have_posts()) : $loop->the_post(); 

     $order_id = $loop->post->ID; 
     $order = new WC_Order($order_id); 
     $product = new WC_Product($order_id); 

// Getting names of items and quantity throught foreach 
foreach($order->get_items() as $item) 
{ 
$item['name']; 
$item['qty']; 
} 

//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'; 
} 
// End email verificacion if is suscribed or not 
?> 

<?php 
     //Colecting all the datas in array 
     // All the information of the customer 
     $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.'', 


     ); 

     //$json = array_map('htmlentities',$json); 
     //$json = html_entity_decode(json_format($json)); 

     //Getting order name and quantity 
     foreach($order->get_items() as $item) 
       { 
       $json['items'][] = array (
       'name' => $item['name'], // Here appear the name of the product 
       'quantity' => $item['qty']); // here the quantity of the product 
       } 
     // I need like this for example in this format 
      foreach($order->get_items2() as $item2) 
       { 
       $json['Sku'][] = array (
       'SKU' => $item2['SKU'], //Here should be appear the SKU from the order 

       } 

    echo (json_format($json)); 

?> 

выход теперь так:

{ 
"salutation": "Prof", 
"title": "frau", 
"first_name": "Maria", 
"last_name": "Schumacher", 
"street": "Insekalm", 
"street_number": "20", 
"address_supplement": "Wagen", 
"zipcode": "SEVILLA", 
"city": "Sevilla", 
"country": "ES", 
"terms_accepted": "true", 
"receiving_mails_accepted": "false", 
"email": "[email protected]", 
"original_created_at": "2014-01-07 04:52:21", 
"items": [ 
    { 
     "name": "Weserbergland", 
     "quantity": "2" 
    } 
]} 

Я пытаюсь сделать этот вывод:

{ 
"salutation": "Prof", 
"title": "frau", 
"first_name": "Maria", 
"last_name": "Schumacher", 
"street": "Insekalm", 
"street_number": "20", 
"address_supplement": "Wagen", 
"zipcode": "SEVILLA", 
"city": "Sevilla", 
"country": "ES", 
"terms_accepted": "true", 
"receiving_mails_accepted": "false", 
"email": "[email protected]", 
"original_created_at": "2014-01-07 04:52:21", 
"items": [ 
    { 
     "SKU": "R2E-3545", 
     "quantity": "2" 
    } 
]}  

ответ

8

Вы можете использовать get_sku() для получения продукта SKU

Заменить это:

//Getting order name and quantity 
foreach($order->get_items() as $item) 
{    { 
    $json['items'][] = array (
     'name' => $item['name'], // Here appear the name of the product 
     'quantity' => $item['qty'], // here the quantity of the product 
    ); 
} 
// I need like this for example in this format 
foreach($order->get_items2() as $item2) 
{ 
    $json['Sku'][] = array (
     'SKU' => $item2['SKU'], //Here should be appear the SKU from the order 
    ); 
} 

С этим:

foreach($order->get_items() as $item) 
{ 
    $json['items'][] = array (
     'SKU' => $product->get_sku(), // Your SKU 
     'name' => $item['name'], // Here appear the name of the product 
     'quantity' => $item['qty'], // here the quantity of the product 
    ); 
} 
+0

Спасибо большое Рахиль! :) – Deimos

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