woocommerce_add_to_cart_form_action
Filters the action URL for the add to cart form before it's submitted.
add_filter( 'woocommerce_add_to_cart_form_action', $callback, 10, 1 );
Description
Filters the action URL for the add to cart form. This hook allows developers to dynamically change where the add to cart form submits its data. It's particularly useful for custom add-to-cart processes or redirecting users to different endpoints after adding a product.
Usage
add_filter( 'woocommerce_add_to_cart_form_action', 'your_function_name', 10, 1 );
Parameters
-
$product(mixed) - This parameter represents the WooCommerce product object being added to the cart.
Return Value
The filtered value.
Examples
/**
* Filters the action URL for the add to cart form to a custom endpoint for specific products.
*
* @param string $url The default add to cart form action URL.
* @param WC_Product $product The product object.
* @return string The modified add to cart form action URL.
*/
add_filter( 'woocommerce_add_to_cart_form_action', function( $url, $product ) {
// Check if the product has a custom meta key indicating it's a special type.
if ( $product->get_meta( '_is_special_order_product' ) === 'yes' ) {
// If it's a special order product, redirect to a custom endpoint for processing.
// This assumes you have a custom endpoint set up at '/special-order-processing/'.
// You would need to handle the logic for this endpoint separately.
return home_url( '/special-order-processing/' );
}
// For all other products, return the original URL.
return $url;
}, 10, 2 ); // Priority 10, accepts 2 arguments.
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:235
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
}
}