Skip to content
Advertisement

Redirect user to other website on when clicked on Add To Cart button

I need help in finding the correct hook for WooCommerce. I’m trying to integrate design-builder API with the WooCommerce store. I have to make a redirect to the builder’s website when a customer clicks “Add To Cart” on the product page. Example URL I need redirect to: https://exampleurl.com/designBuilder/{UniqueID} {UniqueID} is generated with this:

add_filter( 'woocommerce_add_cart_item_data', 'woo_api_add_cart_item_data', 10, 4 );
function woo_api_add_cart_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {

    $product = wc_get_product( $product_id );
    $unique_id = uniqid();

    //...Some code...


    return $cart_item_data;
}

Is there a WooCommerce hook to change the “add to cart” redirection URL and pass some values from the product?

Advertisement

Answer

add_filter('woocommerce_add_to_cart_redirect', 'custom_woocommerce_add_to_cart_redirect', 10, 2);

function custom_woocommerce_add_to_cart_redirect($url, $product) {

    $url = "https://exampleurl.com/designBuilder/{$product->get_id()} ";
    header("Location: $url");
    exit;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement