Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda