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, ...
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, ...
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.
This is a custom field so with
get_post_meta( $product->get_id(), '_unit', true) ,
you can get the unit value
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