2

I want to show product units inside the quantity field (will use CSS to position the unit inside the quantity field).

How can I retrieve the product unit from the product object?

For instance: kg, qm, l, ...

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
cryptocpt
  • 61
  • 1
  • 8

3 Answers3

6

In Woocommerce to get the Weight unit you will use:

$weight_unit = get_option('woocommerce_weight_unit');
echo 'Weight unit: ' . $weight_unit; // testing output;

In Woocommerce to get the Dimension unit you will use:

$dimension_unit = get_option('woocommerce_dimension_unit');
echo 'Dimension unit: ' . $dimension_unit; // testing output;

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Looks good, but the returning values do differ from the value set inside the product. `$dimension_uni` is cm where in the product I've set qm – cryptocpt Feb 19 '18 at 09:35
  • @cryptocpt **That are the general product settings for all products…** Now may be you are using an additional plugin that allow you to change the product "dimension" unit for each product, or something similar… In that case you will have to ask a new question, providing all necessary details about those plugins. My answer here answers your question as it is provided by you. – LoicTheAztec Feb 19 '18 at 09:43
  • Please see image, I have added it to clearify what exactly I'm needing. – cryptocpt Feb 19 '18 at 09:47
  • 1
    @cryptocpt That is maid with **custom code** or a plugin… It's a **custom field** that doesn't exist by default on Woocommerce… If you don't provide the meta_key for that custom field, nobody will be able to answer… Also you should provide all the necessary details, as this seems to be a variable product… If you don't make this effort, your chances to get a useful answer are very poor. People can't guess that. – LoicTheAztec Feb 19 '18 at 10:02
1

This is a custom field so with get_post_meta( $product->get_id(), '_unit', true) , you can get the unit value

cryptocpt
  • 61
  • 1
  • 8
0

To display quantity input fields for simple products within your shop archive pages, you can add the following code to your theme’s functions.php file.

<?php 
/**
 * Code should be placed in your theme functions.php file.
 */
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

Hope this will help

Navnish Bhardwaj
  • 1,687
  • 25
  • 39