Advertisement
Guest User

Untitled

a guest
May 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. function custom_quantity_field_archive() {
  2. $product = wc_get_product( get_the_ID() );
  3. if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
  4. woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
  5. }
  6. }
  7. add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 20, 9 );
  8.  
  9. /**
  10. * Add requires JavaScript.
  11. */
  12. function custom_add_to_cart_quantity_handler() {
  13. wc_enqueue_js( '
  14. jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
  15. return false;
  16. });
  17. jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() {
  18. var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
  19. // For AJAX add-to-cart actions
  20. add_to_cart_button.data( "quantity", jQuery( this ).val() );
  21. // For non-AJAX add-to-cart actions
  22. add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
  23. });
  24. ' );
  25. }
  26. add_action( 'init', 'custom_add_to_cart_quantity_handler' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement