woocommerce_after_add_to_cart_quantity
Fires after the quantity input field is displayed on a product page. Fires after the quantity input field is displayed on a single product page.
add_action( 'woocommerce_after_add_to_cart_quantity', $callback, 10, 1 );
Description
Fires after the quantity input field for a product on single product pages. Use this hook to add custom content or logic, like extra buttons or information, related to the add to cart process before the main add to cart button appears.
Usage
add_action( 'woocommerce_after_add_to_cart_quantity', 'your_function_name', 10, 1 );
Examples
add_action( 'woocommerce_after_add_to_cart_quantity', 'my_custom_stock_display_after_quantity', 10, 2 );
/**
* Displays a custom message or information after the add to cart quantity input.
* This is a practical example demonstrating how to hook into woocommerce_after_add_to_cart_quantity
* to dynamically display additional product information or prompts.
*
* @param int $product_id The ID of the product.
* @param WC_Product $product The product object.
*/
function my_custom_stock_display_after_quantity( $product_id, $product ) {
// Example: Check if the product is in stock and display a specific message if low.
$stock_quantity = $product->get_stock_quantity();
if ( $stock_quantity !== null && $stock_quantity < 5 && $stock_quantity > 0 ) {
echo '<p class="custom-low-stock-alert" style="color: orange; font-weight: bold;">' . esc_html__( 'Hurry, only a few left!', 'your-text-domain' ) . '</p>';
} elseif ( $stock_quantity === 0 ) {
echo '<p class="custom-out-of-stock-alert" style="color: red;">' . esc_html__( 'This item is currently out of stock.', 'your-text-domain' ) . '</p>';
}
// Another example: Display custom information based on product meta.
$custom_info = get_post_meta( $product_id, '_my_custom_product_info', true );
if ( ! empty( $custom_info ) ) {
echo '<div class="custom-product-extra-info">' . wp_kses_post( wpautop( $custom_info ) ) . '</div>';
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/integrations/woocommerce/class-woo-automator-codes.php:249
public function custom_product_add_to_cart() {
global $product;
if ( empty( $product->get_price() ) ) {
if ( absint( $product->get_price() ) < 0 ) {
return;
}
}
// Make sure it's our custom product type.
$codes_available = SharedFunctionality::get_available_codes_by_group_id( $product->get_id() );
if ( $codes_available > 0 ) {
$html = '';
do_action( 'woocommerce_before_add_to_cart_button' );
ob_start();
echo '<form action="' . esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->add_to_cart_url() ) ) . '" class="cart" method="post" enctype="multipart/form-data">';
do_action( 'woocommerce_before_add_to_cart_button' );
do_action( 'woocommerce_before_add_to_cart_quantity' );
echo woocommerce_quantity_input(
array(
'max_value' => $codes_available,
'min_value' => 1,
),
$product,
false
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
echo '<button type="submit" class="' . esc_attr( $this->product_type ) . '_add_to_cart_button button alt" name="add-to-cart" value="' . esc_attr( $product->get_id() ) . '">' . esc_html( $product->add_to_cart_text() ) . '</button>';
do_action( 'woocommerce_after_add_to_cart_button' );
echo '</form>';
do_action( 'woocommerce_after_add_to_cart_form' );
$html = ob_get_clean();
echo apply_filters( 'ulc_modify_add_to_cart', $html );
$display = __( 'In stock', 'woocommerce' );
$stock_amount = $codes_available;
switch ( get_option( 'woocommerce_stock_format' ) ) {
case 'low_amount':
if ( $stock_amount <= wc_get_low_stock_amount( $product ) ) {
/* translators: %s: stock amount */
$display = sprintf( __( 'Only %s left in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
}
break;
case '':
/* translators: %s: stock amount */
$display = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
break;
}
if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
$display .= ' ' . __( '(can be backordered)', 'woocommerce' );
}
?>
<p class="stock instock"><?php echo wp_kses_post( $display ); // WPCS: XSS ok. ?></p>
<?php
}
}