Echo woocommerce 产品缩略图? [英] Echo woocommerce product thumbnail?
问题描述
我正在尝试将产品的 WooCommerce 缩略图显示到订单上.
Im trying to echo WooCommerce thumbail of producs onto an orderform.
虽然不确定如何.这是我到目前为止所得到的:
Not sure how though though. This is what I've got so far:
<?php echo woocommerce_get_product_thumbnail();?>
这给了我 WooCommerce 缩略图占位符.我如何为我的订单中的每个项目提取正确的一个?订单具有订单 ID、创建日期、状态和价格的字段,因此它会在某处提取正确的 ID.
This give me the WooCommerce thumbnail placeholder. How do I pull the right one for each item in my orderform? The orderform has fields for order id, creationdate, status and price, so it does pull the proper id´s somewhere.
这是元键和其他字段的示例,这些字段为同一表单上的每个订单提取信息(如果其中任何一个有意义的话).
This is examples of the meta keys and other fields that pull info for each order on the same form, if any of this makes any sense.
<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 就是这样设置的.我为图像添加了第一个表类.
This is how the php is set up. I added the first table class for the 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>
推荐答案
请记住,WooCommerce 已经在我的帐户"部分中包含以前订单的所有项目/成本/缩略图,客户可以直接从他们的我的"部分再次订购"帐户区域.
Keep in mind that WooCommerce already has all the items/costs/thumbnails for previous orders in the My Accounts section and customers can "order again" directly from their My Account area.
但是,假设您有订单 ID,您可以使用以下内容访问产品缩略图.这将输出一个带有缩略图和产品名称的小项目列表:
But, assuming you have the order ID you can get to the product thumbnails with the following. This will output a small list of items with their thumbnails and product name:
$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;
}
然后你可以像这样在你的模板中使用它:
Then you can use it in your template like so:
<td class="order-image">
<?php echo so_28179558_get_order_thumbnail( $order ); ?>
</td>
这篇关于Echo woocommerce 产品缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!