Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!