woocommerce_before_add_to_cart_quantity
Fires before the quantity input field is displayed on the single product page.
add_action( 'woocommerce_before_add_to_cart_quantity', $callback, 10, 1 );
Description
Fires before the quantity input field on single product pages. Allows developers to inject custom content or modify the quantity input's behavior before it's displayed, potentially for custom product types or price-based logic.
Usage
add_action( 'woocommerce_before_add_to_cart_quantity', 'your_function_name', 10, 1 );
Examples
add_action( 'woocommerce_before_add_to_cart_quantity', 'my_custom_woocommerce_before_add_to_cart_quantity', 10, 0 );
/**
* Example function to add custom text before the add to cart quantity input.
* This function demonstrates how to hook into woocommerce_before_add_to_cart_quantity
* and display dynamic information relevant to the product.
*/
function my_custom_woocommerce_before_add_to_cart_quantity() {
global $product;
// Check if we are on a single product page and if the product exists.
if ( ! is_product() || ! $product ) {
return;
}
// Example: Display a custom message if the product is on sale.
if ( $product->is_on_sale() ) {
echo '<p class="custom-sale-message">' . esc_html__( 'Special Offer: Grab this item at a discounted price!', 'your-text-domain' ) . '</p>';
}
// Example: Display the available stock quantity in a specific format if needed.
$stock_quantity = $product->get_stock_quantity();
if ( $stock_quantity !== null && $stock_quantity > 0 ) {
echo '<p class="custom-stock-info">' . sprintf( esc_html__( 'Currently %s in stock.', 'your-text-domain' ), '<strong>' . absint( $stock_quantity ) . '</strong>' ) . '</p>';
} elseif ( $stock_quantity === 0 ) {
echo '<p class="custom-stock-info">' . esc_html__( 'Currently out of stock.', 'your-text-domain' ) . '</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:238
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
}
}