Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Created April 27, 2018 13:28
Show Gist options
  • Save EricBusch/fa8998989d74426e7c7764415e885d8a to your computer and use it in GitHub Desktop.
Save EricBusch/fa8998989d74426e7c7764415e885d8a to your computer and use it in GitHub Desktop.
Adds an action to the loop to display a [Buy at Acme] button containing the merchant's name to the WooCommerce Product Loop which links to an External/Affiliate product. [datafeedr]
<?php
/**
* Adds an action to the loop to display a [Buy at Acme] button containing the merchant's name
* to the WooCommerce Product Loop which links to an External/Affiliate product.
*/
function mycode_add_more_details_button_to_external_with_merchant_name() {
add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_external_with_merchant_name' );
}
add_action( 'init', 'mycode_add_more_details_button_to_external_with_merchant_name' );
/**
* Display [Buy at Acme] button containing the merchant's name in the
* WooCommerce Product Loop which links to an External/Affiliate product.
*
* @global WC_Product_External $product
*/
function mycode_display_more_details_button_to_external_with_merchant_name() {
/**
* @var WC_Product_External $product
*/
global $product;
if ( 'external' != $product->get_type() ) {
return;
}
$url = $product->get_product_url();
$merchant = $product->get_attribute( 'pa_merchant' );
$text = $merchant ? sprintf( __( 'Buy at %s', 'mycode' ), $merchant ) : __( 'Buy Now', '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" target="_blank">%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