Echo woocommerce产品缩图?

时间:2015-01-27 20:40:32

标签: php wordpress woocommerce

我试图将产品的WooCommerce缩略图反映到订单上。

虽然不确定如何。这是我到目前为止所得到的:

<?php echo  woocommerce_get_product_thumbnail();?>

这给了我WooCommerce缩略图占位符。 如何为订单中的每个项目选择正确的一个?订单表格包含订单ID,创建日期,状态和价格的字段,因此它确实在某处提取了正确的ID。

这是元键和其他字段的示例,它们为同一表单上的每个订单提取信息,如果其中任何一个有意义的话。

 <a href="<?php echo $order->get_view_order_url(); ?>">
  <?php echo $order->get_order_number(); ?>
  foreach ( $customer_orders as $customer_order ) {
      $order      = wc_get_order();
      $order->populate( $customer_order );
      $item_count = $order->get_item_count();
      $customer_orders = get_posts( 
                             apply_filters( 'woocommerce_my_account_my_orders_query', 
                                 array(
                                     'numberposts' => $order_count,
                                     'meta_key'    => '_customer_user',
                                     'meta_value'  => get_current_user_id(),
                                     'post_type'   => wc_get_order_types('view-orders'),
                                     'post_status' => array_keys(wc_get_order_statuses())
                                 ) 
                             ) 
                         );

这就是php的设置方式。我为immage添加了第一个表类。

  <tr>              

        <th class="order-number"><span class="nobr"><?php _e( 'Image', 'woocommerce' ); ?></span></th>

            <th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th>

            <th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th>

  </tr>

然后我试图添加图像。

<td class="order-image”>

<?php  echo woocommerce_get_product_thumbnail(); ?>

</td>



<td class="order-number">

<a href="<?php echo $order->get_view_order_url(); ?>">

<?php echo $order->get_order_number(); ?>

</a>

</td>



<td class="order-date”>

<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>

                </td>

1 个答案:

答案 0 :(得分:1)

请注意,WooCommerce已经拥有“我的帐户”部分中之前订单的所有项目/费用/缩略图,客户可以再次订购&#34;直接从他们的我的帐户区域。

但是,假设您拥有订单ID,您可以使用以下内容访问产品缩略图。这将输出一小部分项目及其缩略图和产品名称:

$order = wc_get_order( $order_id );
$items = $order->get_items();
if( $items ) {
    echo '<ul class="ordered-items">';
    foreach( $items as $item ){
        $id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
        $product = wc_get_product( $id );
        echo '<li>'. $product->get_image() . $product->get_title() . '</li>';
    }
    echo '</ul>';
}

编辑:要集成到您的代码,我可能会执行以下操作

function so_28179558_get_order_thumbnail( $order ){

    if( is_numeric( $order ) ){
        $order = wc_get_order( $order_id );
    }

    if( is_wp_error( $order ) ){
        return;
    }

    $order_thumb = '';

    $items = $order->get_items();
    if( $items ) {
        foreach( $items as $item ){
            $id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
            $product = wc_get_product( $id );
            $order_thumb = $product->get_image();
            continue;
        }
    }

    return $order_thumb;
}

然后您可以在模板中使用它,如下所示:

<td class="order-image”>

<?php echo so_28179558_get_order_thumbnail( $order ); ?>

</td>
相关问题