Skip to content

Instantly share code, notes, and snippets.

@shubhw12
Last active April 13, 2020 05:17
Show Gist options
  • Select an option

  • Save shubhw12/081d643fe4701f56400fba2bac42ca4b to your computer and use it in GitHub Desktop.

Select an option

Save shubhw12/081d643fe4701f56400fba2bac42ca4b to your computer and use it in GitHub Desktop.
Add the quantity button next to add to cart button in astra on shop page
function custom_quantity_field_archive() {
$product = wc_get_product( get_the_ID() );
if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
}
}
add_action( 'astra_woo_shop_add_to_cart_after', 'custom_quantity_field_archive', 0, 9 );
add_action( 'init', 'custom_add_to_cart_quantity_handler' );
/**
* Add requires JavaScript.
*/
function custom_add_to_cart_quantity_handler() {
wc_enqueue_js( '
jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
return false;
});
jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() {
var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
// For AJAX add-to-cart actions
add_to_cart_button.data( "quantity", jQuery( this ).val() );
// For non-AJAX add-to-cart actions
add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
});
' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment