php之按顺序显示项目,包括图像 WooCommerce

soundcode 阅读:86 2025-01-19 22:14:33 评论:0

我正在尝试使用以下 php 显示 WooCommerce 订单中的产品/项目,但未显示这些项目。

我使用的代码是对这个的改编:Get cart item name, quantity all details woocommerce

使用上述问题中的原始代码也不会在我的页面上显示任何内容。

提前致谢。

<?php   
 
 
        global $woocommerce; 
        $items = $woocommerce->cart->get_cart(); 
 
        foreach($items as $item => $values) {  
        $_product = $values['data']->post; 
        //product image 
        $getProductDetail = wc_get_product( $values['product_id'] ); 
        echo '<tr><td>'; 
        echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr ) 
        echo '</td>'; 
        echo '<td>'; 
        echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';  
        echo '</td></tr>'; 
        }; 
   ?> 

完整的页面代码在这里:
<?php 
/* 
Template Name: Store 
*/ 
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.'); 
 
 
?> 
  <!DOCTYPE HTML> 
  <html> 
 
  <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title> 
      <?php _e('Store Queue'); ?> 
    </title> 
 
  </head> 
 
  <body id="driverqueue"> 
    <header> 
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
 
        <h1 class="title"><?php the_title(); ?></h1> 
 
        <?php the_content(); ?> 
 
          <?php endwhile; endif; ?> 
    </header> 
    <section> 
      <?php  
 
  global $woocommerce; 
 
 
  $args = array( 
    'post_type' => 'shop_order', 
    'post_status' => 'wc-processing', 
    'meta_key' => '_customer_user', 
    'posts_per_page' => '-1' 
  ); 
  $my_query = new WP_Query($args); 
 
  $customer_orders = $my_query->posts; 
 
  foreach ($customer_orders as $customer_order) { 
   $order = new WC_Order(); 
 
   $order->populate($customer_order); 
   $orderdata = (array) $order; 
 
   // $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy! 
  } 
 
 
 
 
  $loop = new WP_Query( $args ); 
 
  while ( $loop->have_posts() ) : $loop->the_post(); 
 
 
 
    $order_id = $loop->post->ID; 
 
    $order = new WC_Order($order_id); 
 
    ?> 
 
 
 
 
 
 
 
        <table class="ordertable" id="<?php echo $order_id; ?>"> 
          <tr> 
            <td> 
              <p>Order # 
                <?php echo $order_id; ?> &mdash; 
                  <time datetime="<?php the_time('c'); ?>"> 
                    <?php echo the_time('d/m/Y g:i:s A'); ?> 
                  </time> 
              </p> 
            </td> 
            <td> 
              <?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?> 
            </td> 
            <td> 
              <table> 
                <tr> 
 
                  <?php   
 
 
            global $woocommerce; 
            $items = $woocommerce->cart->get_cart(); 
 
            foreach($items as $item => $values) {  
            $_product = $values['data']->post; 
            //product image 
            $getProductDetail = wc_get_product( $values['product_id'] ); 
            echo '<tr><td>'; 
            echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr ) 
            echo '</td>'; 
            echo '<td>'; 
            echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';  
            echo '</td></tr>'; 
            }; 
              ?> 
                </tr> 
              </table> 
            </td> 
            <td> 
              <p> 
                <?php 
            do_action( 'woocommerce_admin_order_actions_start', $order ); 
 
            $actions = array(); 
 
 
            if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) { 
              $actions['complete'] = array( 
                'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=awaiting-shipment&order_id=' . $post->ID ), 'woocommerce-mark-order-status' ), 
                'name'      => __( 'Complete', 'woocommerce' ), 
                'action'    => "complete" 
              ); 
            }; 
 
            $actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order ); 
 
            foreach ( $actions as $action ) { 
              printf( '<a class="button %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) ); 
            } 
 
            do_action( 'woocommerce_admin_order_actions_end', $order ); 
          ?> 
              </p> 
            </td> 
            <td> 
              <form action=""> 
                <input type="checkbox" class="ordercollected" value="0" /> 
              </form> 
            </td> 
          </tr> 
          <?php endwhile; ?> 
 
    </section> 
  </body> 
 
  </html> 

请您参考如下方法:

使用以下链接中的信息和下面的改编代码解决了这个问题:

https://wordpress.stackexchange.com/questions/180075/how-to-get-woocommerce-order-product-info

<?php   
 
foreach ($order->get_items() as $key => $lineItem) { 
 
        //uncomment the following to see the full data 
               // echo '<pre>'; 
               // print_r($lineItem); 
               // echo '</pre>'; 
                $product_id = $lineItem['product_id']; 
                $product = wc_get_product( $product_id ); 
                echo '<tr><td>' . $product->get_image() . '</td>'; // accepts 2 arguments ( size, attr ) 
                echo '<td>' . 'Product: ' . $lineItem['name'] . '</td>'; 
                echo '<td> x' . $lineItem['qty'] . '</td></tr>'; 
              } 
 
              ?> 


标签:PHP
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号