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".
- 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.
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, $product ) { if ( $product->get_type() != 'external' ) { return $button_text; } if ( ! dfrpswc_is_dfrpswc_product( $product->get_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->get_id(), '_dfrps_product', true ); $merchant_name = esc_html( $meta['merchant'] ); // This is the text that will appear on the button. $name is equal to the merchant's name. $button_text = "Buy from $merchant_name"; return $button_text; }
- 3
-
Save
Save your custom plugin changes.