Override External Product URL to Add to Cart product button
Updated 2020
This is a complete different way with simple products and a custom field external link.
In this answer we will use simple products instead of external products.
- We add an "External URL" custom field in product option settings and we save the data.
Add a custom field on general product option settings for simple products only :
add_action( 'woocommerce_product_options_general_product_data', 'simple_product_with_external_url' );
function simple_product_with_external_url() {
global $product_object;
echo '<div class="options_group show_if_simple hidden">';
// External Url
woocommerce_wp_text_input( array(
'id' => '_ext_url_cust',
'label' => 'External Url',
'description' => 'Custom external URL',
'desc_tip' => 'true',
'placeholder' => 'Enter here your custom external URL'
) );
echo '</div>';
}
Save the custom field data if it's a simple product and not empty:
add_action( 'woocommerce_admin_process_product_object', 'save_simple_product_with_external_url' );
function save_simple_product_with_external_url( $product ) {
if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {
$product->update_meta_data( '_ext_url_cust', sanitize_url($_POST['_ext_url_cust']) );
}
}
2) This will not work on shop pages and archives pages, if we don't set in WooCommerce the cart redirection when adding a product to cart.
So we will replace add-to-cart button (just for our simple products with a custom link redirection) on shop pages and archives pages by a linked custom button to single product pages.
Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$external_url = $product->get_meta('_ext_url_cust');
if ( ! empty($external_url) ) {
$html = sprintf( '<a href="%s" class="button alt add_to_cart_button">%s</a>', $product->get_permalink(), __("Read More", "woocommerce") );
}
return $html;
}
3) If the custom field value is not empty, the product is added to cart first and then redirected to the external URL (our custom field value in single product pages)
External URL redirection after adding to cart (when custom field is not empty in simple products):
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url' );
function redirect_simple_product_with_external_url( $url ) {
if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )
return get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce version 3+
Override External/Affiliate Product Button URL (with Query String Parameters)
Ended up using get_the_title
instead because the_title_attribute
was causing an error (product title text duplicating and rendering outside the button markup). Also added url encoding, final code looks like:
// Override external button url
function override_external_product_url( $url, $product ){
if ( 'external' === $product->get_type() ) {
// custom add to cart url example
$url = home_url("/contact/?your-subject=" . urlencode("Product Enquiry: " . get_the_title()));
}
return $url;
}
add_filter( 'woocommerce_product_add_to_cart_url', 'override_external_product_url', 10, 2 );
Changing add to cart link with custom url in WooCommerce
WooCommerce has this already set up with the "External/Affiliate Product" setting for your product.
See this video on YouTube for an example or View Woo's documentation and video
In short, you put in the product URL after selecting the External/Affiliate Product dropdown option (single product is the default) and then you can customize the Add to cart text.
Related Topics
How to Do a Curl Request to the Same Server
How to Convert Array of Bytes to a String in PHP
Using Utf8Mb4 with PHP and MySQL
Facebook Sdk V4 for PHP Minimal Example
Download of .Zip File Runs a Corrupted File PHP
Tcpdf Error :Unable to Get the Size of the Image
Why Do I Get "Resource Id #4" When I Apply Print_R() to an Array in PHP
Change Innerhtml of a PHP Domelement
How to Have a Stable Sort in PHP with Arsort()
Submit Multiple Forms with One Button
Execute PHP Script Before Every PHP Script
PHP Undefined Index Error $_Files
PHP Date Conversion to Strtotime