Hi, in woocommerce, the built-in function passes the $ quantity variable to the post method, but how do you make your input field so that the value is passed to this code without reloading the page?

echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', esc_url( $product->add_to_cart_url() ), esc_attr( isset( $quantity ) ? $quantity : 1 ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( isset( $class ) ? $class : 'button' ), esc_html( $product->add_to_cart_text() ) ), $product ); 

code that uses woocommerce for input

 woocommerce_quantity_input( array( 'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ), 'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product ), 'input_value' => ( isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ) ); 
  • Do you mean AJAX? - user208916
  • Rephrase, please, the question. It is not entirely clear what you are talking about. If you mean AJAX, this technology is mostly client-side and concerns javascript and some html, and you only show php. - Ivan Pshenitsyn
  • maybe) I don’t know by what I need to make a form for entering a number so that this value is immediately written to a variable and it can be transferred to php code without refreshing the page. The form of the type is <input name = "quantity" value = "1" title = "Amount" class = "input-text qty text" size = "4" pattern = "[0-9] *" inputmode = "numeric "type =" number ">. As I understand it, I need javascript which will transfer the value to a variable, but without needing to reload the page - Svyatoslav Khizhnyak
  • Yes, you need to work with JS. or rather, AJAX, as already noted. And I think you do not fully understand the differences client-server. You need to transfer the variable not to the php-code, but to the server. php is performed on the server, javascript and all input and all html are on the client. - Ivan Pshenitsyn

1 answer 1

solved the problem) here is the script)

 <script> jQuery( document ).ready( function( $ ) { $( document ).on( 'change', '.quantity .qty', function() { $( this ).parent( '.quantity' ).next( '.add_to_cart_button' ).attr( 'data-quantity', $( this ).val() ); }); });