woocommerce_before_add_to_cart_button
Fires just before the Add to Cart button displays on single product pages.
add_action( 'woocommerce_before_add_to_cart_button', $callback, 10, 1 );
Description
Fires before the "Add to Cart" button on single product pages. Use this hook to display custom content, modify the button's appearance, or add extra functionality like custom price calculations or conditional logic before the add-to-cart action. Ensure your callback checks for valid product data and handles potential errors gracefully.
Usage
add_action( 'woocommerce_before_add_to_cart_button', 'your_function_name', 10, 1 );
Examples
// Display a custom message before the add to cart button for specific products.
add_action( 'woocommerce_before_add_to_cart_button', 'my_custom_before_add_to_cart_message', 10 );
/**
* Adds a custom message before the add to cart button.
*
* This function checks if the current product is a "virtual" product
* and if it has a specific meta key set. If both conditions are met,
* it displays a custom message.
*/
function my_custom_before_add_to_cart_message() {
global $product;
// Ensure we have a product object and it's a valid type (e.g., simple or variable).
if ( ! $product || ! is_a( $product, 'WC_Product' ) ) {
return;
}
// Check if the product is virtual and has a specific meta key.
// Replace 'my_special_product' with your actual product identifier or condition.
// Replace 'custom_message_active' with your actual meta key.
if ( $product->is_virtual() && get_post_meta( $product->get_id(), 'custom_message_active', true ) === 'yes' ) {
// Retrieve the custom message from another meta key.
$custom_message = get_post_meta( $product->get_id(), 'custom_product_message', true );
if ( ! empty( $custom_message ) ) {
// Sanitize and display the message.
echo '<div class="custom-add-to-cart-message" style="margin-bottom: 10px; color: #0073aa; font-weight: bold;">' . esc_html( $custom_message ) . '</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:231
src/classes/integrations/woocommerce/class-woo-automator-codes.php:237
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
}
}