woocommerce_after_add_to_cart_form
Fires after the add to cart form is displayed on a single product page.
add_action( 'woocommerce_after_add_to_cart_form', $callback, 10, 1 );
Description
Fires after the WooCommerce add-to-cart form has been rendered. Developers can use this action to add custom content or modify the form, such as displaying additional product information or custom buttons, after the standard add-to-cart elements.
Usage
add_action( 'woocommerce_after_add_to_cart_form', 'your_function_name', 10, 1 );
Examples
add_action( 'woocommerce_after_add_to_cart_form', 'my_custom_add_to_cart_message', 10, 1 );
/**
* Displays a custom message after the add to cart form for specific products.
*
* This function hooks into `woocommerce_after_add_to_cart_form` to check if
* a specific product has a custom meta field set. If it does, it displays
* a user-friendly message indicating availability or a special note.
*
* @param WC_Product $product The current product object.
*/
function my_custom_add_to_cart_message( $product ) {
// Check if the product is of a specific type or has a custom meta key.
// For this example, let's assume we have a meta key '_custom_product_feature'
// that stores a descriptive string if the feature is enabled.
$custom_feature_message = $product->get_meta( '_custom_product_feature', true );
if ( ! empty( $custom_feature_message ) && is_string( $custom_feature_message ) ) {
// Check stock status for a more dynamic message.
if ( $product->is_in_stock() ) {
// Display a message indicating the custom feature is available and the product is in stock.
echo '<div class="custom-product-message" style="margin-top: 10px; color: green; font-weight: bold;">' . esc_html( $custom_feature_message ) . ' - Available now!</div>';
} else {
// Display a message indicating the custom feature is available but the product is out of stock.
echo '<div class="custom-product-message" style="margin-top: 10px; color: orange;">' . esc_html( $custom_feature_message ) . ' - Currently out of stock.</div>';
}
}
// You could also add a general message that applies to all products after the form.
// echo '<p class="general-product-note">Thank you for considering our products!</p>';
}
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:257
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
}
}