Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
woocommerce - increase quantity value by speficic % on a button click

On a site that sells wood floors by the box, I'd need to add on option to add a % of extra material that is recommended, by increasing the quantity value.

What i've tried so far is this snippet that adds specific value to the quantity label and it works:

<a id="quantityincrease" class="button" href="#">Add recommended 
waste</a>
<script>
jQuery( function($) {
$('#quantityincrease').click( function(e) {
    e.preventDefault();
    $('[name="quantity"]').val('150');
    console.log('#quantityincrease was clicked');
});
});
</script>

What I need is that button to increase the value in the quantity / area_needed label by a specific percentage. Thanks in advance to anyone who will try to help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From what I have understood, you need not only javascript but also to add the label on the product page and carry it on to cart data and order. The code can be as follow:

add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
    echo '<div class="area-selection-management">';
        echo '<label for="area_needed_input" id="area-needed-label">-</label>';
        echo '<input type="hidden" name="area_needed_input" id="area-needed-input" value="0">';
    echo '</div>';
    ?>
    <script>
        jQuery(document).ready(function($) {
            $('[name="quantity"]').on('change', function(e) {
            var current_area = $('#area-needed-input').val();
            var increase = 9 // Do your mathematical calculations hereto increase the value accordingly.
                $('#area-needed-input').val(increase);
                $('#area-needed-label').text(increase + 'sq m');
        });
    </script>
}

After add to cart you will need to add that to cart meta which can be done as mentioned below:

add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_data, $product_id ) {
    if( isset( $_POST['area_needed_input'] ) )
        $cart_item_data['area_needed_input'] = sanitize_tex_field( $_POST['area_needed_input'] );
    return $cart_item_data;
}

Then you will need to add the cart meta to order.

add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 3 );
function add_order_item_meta ( $item_id, $cart_item, $cart_item_key ) {
    if ( isset( $cart_item[ 'area_needed_input' ] ) ) {
        wc_add_order_item_meta( $item_id, __( "Area Needed", "woocommerce"), sanitize_text_field( $cart_item[ 'area_needed_input' ] ) );
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda