Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active July 11, 2019 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricBusch/af1ff0339db47728eae3b103b9251101 to your computer and use it in GitHub Desktop.
Save EricBusch/af1ff0339db47728eae3b103b9251101 to your computer and use it in GitHub Desktop.
Replace [Buy product] button with [More details] button for WooCommerce External/Affiliate links
add_action( 'woocommerce_before_shop_loop_item', function () {
global $product;
if ( 'external' == $product->get_type() ) {
add_action( 'woocommerce_after_shop_loop_item', 'mycode_more_details_button' );
}
} );
add_action( 'woocommerce_before_shop_loop_item', function () {
global $product;
if ( 'external' == $product->get_type() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
} );
add_action( 'woocommerce_after_shop_loop_item', function () {
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_after_shop_loop_item', 'mycode_more_details_button' );
}, 20 );
function mycode_more_details_button() {
global $product;
$url = $product->get_permalink();
$text = __( 'More details', 'mycode' );
$title = sprintf( __( 'View more details about %s', 'mycode' ), $product->get_name() );
$class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() );
$format = '<a href="%1$s" title="%2$s" class="%3$s">%4$s</a>';
printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment