WooCommerce-显示特定产品的订单?

时间:2015-07-13 作者:Thiago

如何使用sku或产品名称查看WooCommerce中特定产品的订单?到目前为止,我所知道的是,但它不起作用。。有人知道怎么做吗?

<?php
   $args = array(
    \'post_type\'       =>\'shop_order\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' =>  50,
    \'order\'          => \'DESC\',
    \'item_meta\' =>  array (
    \'_sku\' => \'ABCD\',
    ),              
    \'tax_query\' => array( 
     array( \'taxonomy\' => \'shop_order_status\',
         \'field\' => \'slug\',
         \'terms\' => array (\'Pending\' , \'Failed\' , \'Processing\' , \'Completed\', \'On-Hold\' , \'Cancelled\' , \'Refunded\')
        ) 
      )             
  );
?>             
<table id="tblExport"  class="demotable1" style="border:1px solid black; ">
            <thead>
                <tr>
                    <th ><?php _e(\'ID:\', \' \'); ?></th>
                    <th ><?php _e(\'sku:\', \' \'); ?></th>
                    <th ><?php _e(\'Categories:\', \' \'); ?></th>
                    <th ><?php _e(\'Product:\', \' \'); ?></th>
                    <th ><?php _e(\'Date:\', \' \'); ?></th>
                    <th ><?php _e(\'Value:\', \' \'); ?></th>
                    <th ><?php _e(\'Name:\', \' \'); ?></th>
                    <th ><?php _e(\'E-mail:\', \' \'); ?></th>
                    <th ><?php _e(\'status:\', \' \'); ?></th>
                </tr>
            </thead>
            <tbody id="export-pla"  >      
                <?php
                $loop = new WP_Query( $args  );
                while ( $loop->have_posts() ) : $loop->the_post(); 
                $order_id = $loop->post->ID; 
                $order = new WC_Order($order_id);
                ?> 
                <tr>
                     <td>
                        <?php
                             //ID - order
                            if ($order->id) : ?><?php echo $order->id; ?><?php endif;?>
                    </td>

                    <td>
                        <?php
                            //SKU
                            if (sizeof($order->get_items())>0)  { foreach($order->get_items() as $item) 
                            { $_product = get_product( $item[\'product_id\'] );   echo \'\' . $_product->sku . \'\';   }  }
                        ?>
                    </td>
                    <td>
                        <?php
                            // Categories
                            if (sizeof($order->get_items())>0)  { foreach($order->get_items() as $item)
                            { $_product = get_product( $item[\'product_id\'] ); 
                            echo $_product->get_categories( \', \', \'\' . _n( \'\', \'\', $size, \'woocommerce\' ) . \' \', \'  \' ); } }
                        ?>
                    </td>

                    <td>
                        <?php
                          // product name
                           if (sizeof($order->get_items())>0)   { foreach($order->get_items() as $item) 
                           { $_product = get_product( $item[\'product_id\'] ); echo \'\' . $item[\'name\'] . \'\';  } }
                        ?>
                    </td>

                    <td>
                        <?php echo the_time(\'d/m/Y\'); ?>
                    </td>
                    <td>
                        <?php if ($order->order_total): $preco_format=($order->order_total);?>
                        <?php echo $trata_preco=number_format($preco_format, 2, ",", "."); ?><?php endif; ?>
                    </td>

                    <td>
                        <?php if ($order->billing_first_name) : ?><?php echo $order->billing_first_name; ?><?php endif; ?>
                        <?php if ($order->billing_last_name) : ?><?php echo $order->billing_last_name; ?><?php endif; ?>
                    </td>

                    <td>
                        <?php if ($order->billing_email) : ?><?php echo $order->billing_email; ?><?php endif; ?>
                    </td>


                    <td>
                        <?php if ($order->status) : ?><?php echo $order->status; ?><?php endif; ?>
                    </td>

               </tr> 
                   <?php endwhile; ?>
                   <?php wp_reset_query(); ?> 
            </tbody>
        </table>

2 个回复
最合适的回答,由SO网友:Thiago 整理而成

Eric基于我为满足我的需求而编写的代码,针对那些想要遵循以下要求的人:

Eric com base em seu código eu escrevi o para atender as minhas necessidades,para quem quiser segue abaixo:

<?php
global $wpdb;   
$produto_id = 22777; // ID do produto
$consulta = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta woim 
        LEFT JOIN {$wpdb->prefix}woocommerce_order_items oi 
        ON woim.order_item_id = oi.order_item_id 
        WHERE meta_key = \'_product_id\' AND meta_value = %d
        GROUP BY order_id;";
$order_ids = $wpdb->get_col( $wpdb->prepare( $consulta, $produto_id ) );
if( $order_ids ) {
                 $args = array(
                \'post_type\'       =>\'shop_order\',
                \'post__in\'   => $order_ids,
                \'post_status\'    => \'publish\',
                \'posts_per_page\' =>  20,
                \'order\'          => \'DESC\',             
                \'tax_query\' => array( 
                array( \'taxonomy\' => \'shop_order_status\',
                       \'field\' => \'slug\',
                       \'terms\' => array (\'Pending\' , \'Failed\' , \'Processing\' , \'Completed\', \'On-Hold\' , \'Cancelled\' , \'Refunded\')
                      ) 
                    )           
                );
                $orders = new WP_Query( $args );
                }
?>               
<table>
            <thead>
                <tr>
                    <th ><?php _e(\'ID do Pedido:\', \'\'); ?></th>
                    <th ><?php _e(\'sku:\', \'\'); ?></th>
                    <th ><?php _e(\'Categoria:\', \'\'); ?></th>
                    <th ><?php _e(\'Produto:\', \'\'); ?></th>
                    <th ><?php _e(\'Data Consolidada da compra:\', \'\'); ?></th>
                    <th ><?php _e(\'Valor:\', \'\'); ?></th>
                    <th ><?php _e(\'Nome:\', \'\'); ?></th>
                    <th ><?php _e(\'Tel:\', \'\'); ?></th>
                    <th ><?php _e(\'End:\', \'\'); ?></th>
                    <th ><?php _e(\'Cidade:\', \'\'); ?></th>
                    <th ><?php _e(\'Estado:\', \'\'); ?></th>
                    <th ><?php _e(\'E-mail:\', \'\'); ?></th>
                    <th ><?php _e(\'status:\', \'\'); ?></th>
                </tr>
            </thead>
            <tbody>      
                <?php
                while ( $orders->have_posts() ) : $orders->the_post(); 
                $order_id = $orders->post->ID; 
                $order = new WC_Order($order_id);            
                ?> 
                <tr>
                     <td>
                        <?php
                           //  Exibe o ID do pedido 
                         if ($order->id) : ?>
                            <a href="<?php echo esc_url( home_url(\'/\' ) ); ?>wp-admin/post.php?post=<?php echo $order->id; ?>&action=edit" target="_blank"><?php echo $order->id; ?></a><?php endif;?>
                    </td>
                    <td>
                        <?php
                            // Exibe o SKU
                            if (sizeof($order->get_items())>0)  { foreach($order->get_items() as $item) 
                            { $_product = get_product( $item[\'product_id\'] );   echo \'\' . $_product->sku . \'\';   }  }
                        ?> 
                    </td>
                    <td>
                        <?php
                            // Exibe a Categoria do produto
                            if (sizeof($order->get_items())>0)  { foreach($order->get_items() as $item)
                            { $_product = get_product( $item[\'product_id\'] ); 
                            echo $_product->get_categories( \', \', \'\' . _n( \'\', \'\', $size, \'woocommerce\' ) . \' \', \'  \' ); } }
                        ?>
                    </td>

                    <td>
                        <?php
                            // Exibe o nome do produto
                           if (sizeof($order->get_items())>0)   { foreach($order->get_items() as $item) 
                           { $_product = get_product( $item[\'product_id\'] ); echo \'\' . $item[\'name\'] . \'\';  } }
                        ?>
                    </td>                   
                    <td>
                        <?php 
                        // Exibe a data da copmpra
                         echo the_time(\'d/m/Y\'); ?>
                    </td>
                    <td>
                        <?php 
                        // Exibe o valor da compra + a formatação do preço
                        if ($order->order_total): $preco_format=($order->order_total);?>
                        <?php echo $trata_preco=number_format($preco_format, 2, ",", "."); ?><?php endif; ?>
                    </td>
                    <td>
                        <?php 
                         // Exibe o nome e sobrenome do cliente
                         if ($order->billing_first_name) : ?><?php echo $order->billing_first_name; ?><?php endif; ?>
                        <?php if ($order->billing_last_name) : ?><?php echo $order->billing_last_name; ?><?php endif; ?>
                    </td>
                  <td>
                        <?php 
                         // Exibe o telefone do cliente
                         if ($order->billing_phone) : ?><?php echo $order->billing_phone; ?><?php endif; ?>
                    </td>
                    <td>
<?php // Exibe o endereço do cliente
 if ($order->billing_address_1): ?><?php echo $order->billing_address_1; ?>, <?php endif; ?> <?php if ($order->billing_number): ?> <?php echo $order->billing_number; ?><?php endif; ?> <?php if ($order->billing_address_2): ?><?php echo $order->billing_address_2; ?> <?php endif; ?><?php if ($order->billing_neighborhood): ?>Bairro: <?php echo $order->billing_neighborhood; ?><?php endif; ?> <?php if ($order->billing_postcode): ?>Cep: <?php echo $order->billing_postcode; ?><?php endif; ?>
                    </td>               
                    <td>
                        <?php 
                        // Exibe a cidade do cliente
                        if ($order->billing_city): ?><?php echo $order->billing_city; ?><?php endif; ?>
                    </td>
                    <td>
                        <?php 
                        // Exibe o estado do cliente
                        if ($order->billing_state): ?><?php echo $order->billing_state; ?><?php endif; ?>
                    </td>
                    <td>
                        <?php 
                        // Exibe o e-mail do cliente
                         if ($order->billing_email) : ?><?php echo $order->billing_email; ?><?php endif; ?>
                    </td>
                    <td>
                        <?php 
                        // Exibe o status do pedidodo cliente
                         if ($order->status) : ?><?php echo $order->status; ?><?php endif; ?>
                    </td>
               </tr> 

            <?php endwhile; ?>
                   <?php wp_reset_query(); ?> 
            </tbody>
        </table>

SO网友:Ericc Antunes

菲托!

Realmente com o WP\\u Query não dá。。。pra saber essa informaçãoénecessário utilizar 2 tabelas do WooCommerce。

montei a funão pra vocè。。。ela busca pelo postID do produto e RETRONA o objeto dos pedidos pra vocêfazer o loop normal。

Ésócoloca la no seu函数。php e usa la em qualquer lugar que quiser!阿奎芬齐奥!这是我的错误,我是一个绅士!

function retorna_idpedidos_por_produtoid($produtoId) {
global $wpdb;
$tabelaOrderItemMeta = $wpdb->prefix . \'woocommerce_order_itemmeta\';
$tabelaOrderItems = $wpdb->prefix . \'woocommerce_order_items\';

$resultadoSelect = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT b.order_id
           FROM {$tabelaOrderItemMeta} a, {$tabelaOrderItems} b
          WHERE a.meta_key = \'_product_id\'
            AND a.meta_value = %s
            AND a.order_item_id = b.order_item_id
            ORDER BY b.order_id DESC",
        $produtoId
    )
);

if($resultadoSelect)
{
    $resultado = array();

    foreach($resultadoSelect as $item)
        array_push($resultado, $item->order_id);

    if($resultado)
    {
        $args = array(
            \'post_type\' =>\'shop_order\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'post__in\' => $resultado,
            \'order\' => \'DESC\'
        );
        $query = new WP_Query($args);

        return $query;
    }
} }
Pronto、agoraésófazer o loop、por Examplo:

<?php $pedidos = retorna_idpedidos_por_produtoid(33); ?>
    <?php if($pedidos && $pedidos->have_posts()): ?>
        <?php while ($pedidos->have_posts()): $pedidos->the_post(); ?>
            <pre>
                <?php the_ID(); ?>
            </pre>
        <?php endwhile ?>
    <?php endif; ?>
Teste aí,espero que dècerto!Abs!

结束

相关推荐

Custom Loop Event Page

我需要在事件页面中创建一个循环,每页分页10篇文章。我想用的方法有点复杂。例如,当前日期为2015年5月1日:2015年1月1日至2015年3月1日至2015年6月1日至2015年9月1日我想这样列出我的所有事件:(第一:ASC排序的未来事件)-(第二:DESC排序的过去事件)因此,循环的最终结果是:2015年6月1日至2015年9月1日/-2015年3月1日至2015年1月1日$current_date = current_time( \'timestamp\', true ); $page =