Add merchant name to "Buy" button

This article explains how to change your "Buy" buttons to read "Buy from {Merchant Name}". For example, if the merchant's name is PetSmart, your "Buy" buttons will read "Buy from PetSmart".

Change "Buy" button to contain merchant's name.
1

Create a custom plugin

If you haven't done so already, create a custom plugin.

2

Add custom code

Add the following code to your custom plugin file.

Tip

If you also want the merchant names to appear in buttons on your store's front page and on category pages, delete lines #14 and #15 from the code below.

/**
 * Change "Buy" button to "Buy from {Merchant Name}" on single product pages with the
 * option to display the merchant's names on all pages.
 */
add_filter( 'woocommerce_product_add_to_cart_text', 'mycode_product_add_to_cart_text_add_merchant_name', 20, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'mycode_product_add_to_cart_text_add_merchant_name', 20, 2 );
function mycode_product_add_to_cart_text_add_merchant_name( $button_text, $type ) {

	global $product;

	if ( $type->product_type != 'external' ) { return $button_text; }
	if ( ! dfrpswc_is_dfrpswc_product( $product->id ) ) { return $button_text; }

	// Remove the following line to display merchant names in buttons on category pages and shop front page.
	if ( ! is_product() ) { return $button_text; }

	$meta = get_post_meta( $product->id, '_dfrps_product', true );
	$name = $meta['merchant'];

	// This is the text that will appear on the button. $name is equal to the merchant's name.
	$button_text = "Buy from $name";

	return $button_text;
}
	
3

Save

Save your custom plugin changes.

Still need help? Contact Us Contact Us