PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to show the formatted weight from WooCommerce product weight value

 May 09, 2022     php, product, woocommerce, wordpress     No comments   

Issue

I have the following code which works very well, it shows me the text (CARRIER) only if the meta field of the order has a value, otherwise it doesn't show the text:

 <?php
    $text_order_meta = get_post_meta($order->get_order_number(), 'carrier_name', true);

    if( ! empty($text_order_meta) )
    { ?> <p> <?php printf( '<b>CARRIER:</b> ' . esc_html( '%s', 'woocommerce' ), esc_html($text_order_meta)  );?> 
</p> <?php 
    }
    ?>

I want to do the something similar to display order items weight.

I know how to get the weight of each product, and to display it only if it has a value:

<?php $product; $attributes = $product->get_attributes(); if ( $product->has_weight() ) { echo $product->get_weight(); } ?>

How I get and display order items weight in WooCommerce?


Solution

You need to use wc_format_weight() function to get the formatted weight, so just replace:

echo $product->get_weight();

with:

echo wc_format_weight($product->get_weight()); 


Answered By - LoicTheAztec
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing