簡體   English   中英

從每個訂單Woocommerce獲取sku

[英]Get sku from each orders 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"
    }
]}    

您可以使用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
   );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM