Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EricBusch/8d6602d1a9091af06ddbab74a462c01d to your computer and use it in GitHub Desktop.
Save EricBusch/8d6602d1a9091af06ddbab74a462c01d to your computer and use it in GitHub Desktop.
Adds an action to the loop to display a [Buy $199.99] button containing the price to the WooCommerce Product Loop which links to an External/Affiliate product. [datafeedr]
<?php
/**
* Adds an action to the loop to display a [Buy $199.99] button containing the price
* to the WooCommerce Product Loop which links to an External/Affiliate product.
*/
function mycode_add_more_details_button_to_external_with_price() {
add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_external_with_price' );
}
add_action( 'init', 'mycode_add_more_details_button_to_external_with_price' );
/**
* Display [Buy $199.99] button HTML containing the price with a link to an External/Affiliate product.
*
* @global WC_Product_External $product
*/
function mycode_display_more_details_button_to_external_with_price() {
/**
* @var WC_Product_External $product
*/
global $product;
if ( 'external' != $product->get_type() ) {
return;
}
$url = $product->get_product_url();
$price = wc_price( $product->get_price() );
$text = sprintf( __( 'Buy %s', 'mycode' ), $price );
$title = sprintf( __( 'Buy %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 ), $text );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment