Deimos Deimos - 1 year ago 114
PHP Question

Get sku from each orders Woocommerce

I'm trying to get the sku from orders of Woocoomerce (Plugin from Wordpress e-commerce)
I need to put the name of the product order, (but this i have it now, only the sku is the problem )
This is my code,

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

?>


The output now is like this:

{
"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"
}
]}


I trying to make this output:

{
"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"
}
]}

Answer Source

You can use get_sku() to retrieve a product SKU

Replace this:

//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
    );
}

With this:

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
   );
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download