How to show attributes in wc_get_orders custom loop?
-
I have to show the name of product and product attributes like that:
Product name
Color: Green
Size: XS.
My code of the loop now is like that. For each status, that’s the example of ‘completed’, but with other statuses loops are the same<section class=”order”>
<?php
$orders = wc_get_orders( array(
‘numberposts’ => -1,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘customer_id’ => get_current_user_id(),
‘status’ => array(‘completed’),
) );//* Loop through each WC_Order object
foreach( $orders as $order ){?>
<div class=”order-wrapper product d-flex col-12″>
<?php
$order_data = $order->get_data(); // The Order data
$order_id = $order_data[‘id’];
$order_currency = $order_data[‘currency’];
$order_status = $order_data[‘status’];
?><div class=”order-number”>#<?php echo $order_id;?></div>
<div class=”order-inner”>
<div class=”order-inner-top”>
<?php
foreach ($order->get_items() as $key => $lineItem) {
$product_id = $lineItem[‘product_id’];
$product = wc_get_product( $product_id );
$name = $lineItem->get_name();
$item_meta_data = $lineItem->get_meta_data();
?>
<div class=”order-inner-top-inner”>
<div class=”order-slider-inner”>
<div class=”order-inner-left”>
<div class=”order-image”>
<?php echo $product->get_image([‘322’, ‘304’]);?>
</div>
</div>
<div class=”order-inner-right”>
<div class=”order-info-item order-info-item-name”>
<?php echo $name; ?>
</div>
<div class=”order-info-item order-info-item “>
<span class=”order-price”><?php echo $lineItem[‘total’] . $order_currency?></span>
</div>
<div class=”order-item-quantity”><?php esc_html_e( ‘Quantity’, ‘woocommerce’ )?>: <?php echo $lineItem[‘qty’]?></div>
</div>
</div>
</div>
<?php } ?>
</div><div class=”order-inner-bottom”>
<div class=”d-flex justify-content-center”>
<button class=”order-total”><?php echo get_theme_mod(‘orders_total_button’);?></button>
</div>
<div class=”totals-toggle”>
<div class=”order-info-item bottom”> <span> <?php esc_html_e( ‘Price’, ‘woocommerce’ )?>:</span><span class=”order-total-price”> <?php echo $order->get_total() . ‘ ‘ . $order_currency; ?></span></div>
<div class=”order-info-item bottom”> <span> <?php esc_html_e( ‘Quantity’, ‘woocommerce’ )?>:</span> <?php echo $order->get_item_count(); ?></div>
<div class=”order-info-item bottom”> <span><?php esc_html_e( ‘Status’, ‘woocommerce’ )?>:</span> <?phpif( ‘completed’== $order->get_status() ) {
echo _x( ‘Completed’, ‘Order status’, ‘woocommerce’ );
}
?></div>
<div class=”order-info-item bottom”> <span><?php esc_html_e( ‘Order Date’, ‘woocommerce’ )?></span> <?php
if( $date_created = $order->get_date_created() ){
// Display the localized formatted date
$formated_date_created = $date_created->date_i18n(‘d.m.Y ‘);
echo $formated_date_created;
}?></div>
<div class=”order-info-item bottom”> <span><?php echo get_theme_mod(‘delivery_date_text’)?>: </span>
<?php
// The orders date
$date_created = $order->get_date_created();
$date_created = $date_created->date(‘d.m.Y’);
// The order date + 5 days
$delivery_date = date_i18n( ‘d.m.Y’, strtotime( $date_created . ‘ +21 days’ ));
echo $delivery_date;
?>
</div>
</div>
</div></div>
</div>
<?php }?></section>`
The name in that way is showing like Name,Green,XS - as one string. If to change $name = $lineItem->get_name(); to $name = $product->get_name(); it's showing only product name. I tried to use
$name = $product->get_name(); $colormeta = $lineItem->get_meta( 'pa_color', true ); $sizemeta = $lineItem->get_meta( 'pa_size', true ); echo $name . ' <br>'; echo 'Color:'. ' ' . $colormeta;
But then attributes are showed not like Green,not by name, they show like green-en, green-ru = by slug, not by name.I use hyyyan polylang. I've found the decision for the cart page, that I needed, I need the same for orders
function custom_product_variation_title($should_include_attributes, $product){ $should_include_attributes = false; return $should_include_attributes; } add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
`
How can I show the attributes in the order loop like Product name Color: Green Size: XS correctly?The page I need help with: [log in to see the link]
- The topic ‘How to show attributes in wc_get_orders custom loop?’ is closed to new replies.