Easily Retrieve Product Weight and Dimension Units in WooCommerce
As an ecommerce store owner using WooCommerce, it's important to accurately display the weight and dimensions of your products to provide a great shopping experience for your customers. Fortunately, WooCommerce makes it easy to retrieve the weight and dimension units set in your store's settings.
In this blog post, we'll walk through the step-by-step process of retrieving the weight and dimension units in WooCommerce using simple PHP code. This will allow you to display the correct units on your product pages and ensure your customers have all the information they need to make informed purchase decisions.
Retrieve the Weight Unit in WooCommerce
To get the weight unit in WooCommerce, you can use the following code:
$weight_unit = get_option('woocommerce_weight_unit');
echo 'Weight unit: ' . $weight_unit;
This code retrieves the weight unit set in your WooCommerce store's settings and stores it in the $weight_unit
variable. You can then use this variable to display the weight unit on your product pages.
For example, you might display the weight of a product like this:
$product_weight = 2.5;
$weight_unit = get_option('woocommerce_weight_unit');
echo 'Product weight: ' . $product_weight . ' ' . $weight_unit;
This would output something like "Product weight: 2.5 kg" or "Product weight: 2.5 lbs", depending on the weight unit set in your WooCommerce settings.
Retrieve the Dimension Unit in WooCommerce
Similarly, to get the dimension unit in WooCommerce, you can use the following code:
$dimension_unit = get_option('woocommerce_dimension_unit');
echo 'Dimension unit: ' . $dimension_unit;
This code retrieves the dimension unit set in your WooCommerce store's settings and stores it in the $dimension_unit
variable. You can then use this variable to display the dimension unit on your product pages.
For example, you might display the dimensions of a product like this:
$product_length = 10;
$product_width = 5;
$product_height = 3;
$dimension_unit = get_option('woocommerce_dimension_unit');
echo 'Product dimensions: ' . $product_length . ' x ' . $product_width . ' x ' . $product_height . ' ' . $dimension_unit;
This would output something like "Product dimensions: 10 x 5 x 3 cm" or "Product dimensions: 10 x 5 x 3 in", depending on the dimension unit set in your WooCommerce settings.
Why is it Important to Display the Correct Units?
Displaying the correct weight and dimension units on your product pages is important for several reasons:
-
Transparency and trust: By providing accurate and transparent information about your products, you build trust with your customers and demonstrate that you value their needs.
-
Informed purchasing decisions: Customers need to know the weight and dimensions of a product to determine if it will fit their needs or requirements. Displaying the correct units helps them make informed purchasing decisions.
-
Compliance and legal requirements: In many regions, there are legal requirements for displaying product weight and dimensions in specific units. Failing to comply with these requirements could result in fines or other penalties.
-
Improved customer experience: A seamless and informative customer experience is crucial for driving sales and building loyalty. Clearly displaying the correct weight and dimension units is a simple way to enhance the overall experience for your customers.
Customizing the Unit Display
While the code we've provided so far works great for basic use cases, you may want to further customize how the weight and dimension units are displayed on your product pages. Here are a few examples:
Displaying the Unit Symbol
Instead of just displaying the unit name, you may want to display the actual unit symbol, such as "kg" or "cm". You can do this by using an array of unit symbols and matching the stored unit value to the corresponding symbol:
$weight_unit = get_option('woocommerce_weight_unit');
$weight_unit_symbols = array(
'kg' => 'kg',
'g' => 'g',
'lbs' => 'lbs',
'oz' => 'oz'
);
$weight_unit_symbol = $weight_unit_symbols[$weight_unit];
echo 'Product weight: 2.5 ' . $weight_unit_symbol;
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
Displaying a Custom Unit Label
Perhaps you want to display a more user-friendly label for the unit, such as "Kilograms" instead of just "kg". You can create an array of custom unit labels and use that instead:
$weight_unit = get_option('woocommerce_weight_unit');
$weight_unit_labels = array(
'kg' => 'Kilograms',
'g' => 'Grams',
'lbs' => 'Pounds',
'oz' => 'Ounces'
);
$weight_unit_label = $weight_unit_labels[$weight_unit];
echo 'Product weight: 2.5 ' . $weight_unit_label;
Handling Multiple Unit Types
If you need to display both weight and dimension units on the same page, you can extend the code to handle both types:
// Get the weight unit
$weight_unit = get_option('woocommerce_weight_unit');
$weight_unit_symbols = array(
'kg' => 'kg',
'g' => 'g',
'lbs' => 'lbs',
'oz' => 'oz'
);
$weight_unit_symbol = $weight_unit_symbols[$weight_unit];
// Get the dimension unit
$dimension_unit = get_option('woocommerce_dimension_unit');
$dimension_unit_symbols = array(
'cm' => 'cm',
'm' => 'm',
'mm' => 'mm',
'in' => 'in',
'ft' => 'ft'
);
$dimension_unit_symbol = $dimension_unit_symbols[$dimension_unit];
// Display the product details
echo 'Product weight: 2.5 ' . $weight_unit_symbol . '<br>';
echo 'Product dimensions: 10 x 5 x 3 ' . $dimension_unit_symbol;
By understanding how to retrieve and display the weight and dimension units in WooCommerce, you can ensure your customers have all the information they need to make informed purchasing decisions. This will not only improve the overall customer experience, but it can also help build trust and increase sales for your online store.
If you're looking for a comprehensive solution to track and analyze your WooCommerce store's performance, consider checking out Flowpoint.ai. Flowpoint can help you identify technical issues, UX/UI problems, and content optimization opportunities that are impacting your conversion rates, and provide data-driven recommendations to fix them