从每个订单中获取 sku Woocommerce [英] Get sku from each orders Woocommerce

查看:23
本文介绍了从每个订单中获取 sku Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

?>

现在的输出是这样的:

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

You can use get_sku() to retrieve a product 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
   );
}

这篇关于从每个订单中获取 sku Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆