I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that change Default Button text for each product, to Add to cart.
function sv_wc_external_product_button( $button_text, $product ) {
if ( 'external' === $product->get_type() ) {
// enter the default text for external products
return $product->button_text ? $product->button_text : 'Add To Cart';
}
return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'sv_wc_external_product_button', 10, 2 );
But this function not add product to cart.
How to make this function to Add selected product to cart?
Thanks.
I fixed myself. For External products, to replace default "Buy This Product" with other generic text, add this functions into functions.php file into theme:
and this one:
to replace text everywhere.
Add a custom field in simple product general option settings:
Save the custom field data if it's a simple product and not empty:
Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):
External URL redirection after adding to cart (when custom field is not empty in simple products):
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 2.6.x
You should try to use
woocommerce_product_add_to_cart_url
filter hook to change the add-to-cart link (here for grouped products), this way:Code goes in function.php file of your active child theme (or theme) or also in any plugin file.