从每个订单Woocommerce获取sku

时间:2014-01-14 22:16:53

标签: php wordpress woocommerce

我正在尝试从Woocoomerce的订单中获取sku(来自Wordpress电子商务的插件) 我需要输入产品订单的名称,(但我现在有了,只有sku是问题) 这是我的代码,

<?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": "tdka@as.com",
"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": "tdka@as.com",
"original_created_at": "2014-01-07 04:52:21",
"items": [
    {
        "SKU": "R2E-3545",
        "quantity": "2"
    }
]}    

1 个答案:

答案 0 :(得分: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
   );
}