woocommerce_after_add_to_cart_button
Fires after the add to cart button is displayed on single product pages.
add_action( 'woocommerce_after_add_to_cart_button', $callback, 10, 1 );
Description
Fires after the "Add to Cart" button on single product pages. Developers can use this hook to inject custom content or modify the display of the add to cart area. It's specifically useful for displaying product-specific information or actions related to the add to cart process, but only if the product has a price.
Usage
add_action( 'woocommerce_after_add_to_cart_button', 'your_function_name', 10, 1 );
Examples
<?php
/**
* Add custom stock message below the add to cart button.
*
* This function demonstrates how to hook into `woocommerce_after_add_to_cart_button`
* to display additional information about product stock.
*
* @param int $product_id The ID of the product being added to the cart.
* @param WC_Product $product The WC_Product object.
*/
function my_custom_stock_message_after_add_to_cart_button( $product_id, $product ) {
// Get the current stock quantity for the product.
$stock_quantity = $product->get_stock_quantity();
// Only display the message if the product is in stock and has a positive stock quantity.
if ( $stock_quantity !== null && $stock_quantity > 0 ) {
$message = sprintf(
__( 'Only %s units remaining!', 'your-text-domain' ),
'<strong class="stock-remaining">' . esc_html( $stock_quantity ) . '</strong>'
);
// Output the custom message.
echo '<div class="custom-stock-message">' . wp_kses_post( $message ) . '</div>';
} elseif ( $stock_quantity === null ) {
// If stock management is disabled, show a general "In Stock" message.
echo '<div class="custom-stock-message"><p class="stock in-stock">' . esc_html__( 'In Stock', 'your-text-domain' ) . '</p></div>';
}
}
// Hook the function to woocommerce_after_add_to_cart_button.
// The '20' is the priority, and '2' indicates that the function accepts two arguments ($product_id, $product).
add_action( 'woocommerce_after_add_to_cart_button', 'my_custom_stock_message_after_add_to_cart_button', 20, 2 );
?>
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:253
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
}
}