Quantity inputs for woocommerce on shop page

<?php 
//Quantity inputs for woocommerce on shop page


// Ensure WooCommerce is active
if ( class_exists( 'WooCommerce' ) ) {
// Ensure Ajax add to cart is enabled
function custom_ajax_add_to_cart() {
if ( is_shop() || is_product_category() || is_product_tag() ) {
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart', 10, 2 );
}
}
add_action( 'woocommerce_before_shop_loop', 'custom_ajax_add_to_cart' );

function quantity_inputs_for_woocommerce_loop_add_to_cart( $html, $product ) {
if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
}

?>

Leave a comment