Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EricBusch/4f3f8ad1f025ad95af295304417552ff to your computer and use it in GitHub Desktop.
Save EricBusch/4f3f8ad1f025ad95af295304417552ff to your computer and use it in GitHub Desktop.
Add highlighting to an add to cart button for WooCommerce products which are on sale. [datafeedr]
<?php
/**
* Add highlighting to an add to cart button for WooCommerce products which are on sale.
*
* @param string $html
* @param WC_Product $product
* @param array $args
*
* @return string
*/
function mycode_display_highlighted_add_to_cart_button_for_products_on_sale( $html, $product, $args ) {
if ( ! $product->is_on_sale() ) {
return $html;
}
$extra_class = ' alt';
$extra_text = ' SALE';
$url = $product->add_to_cart_url();
$quantity = isset( $args['quantity'] ) ? $args['quantity'] : 1;
$class = isset( $args['class'] ) ? $args['class'] . $extra_class : 'button' . $extra_class;
$attributes = isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '';
$text = $product->add_to_cart_text() . $extra_text;
$format = '<a href="%1$s" data-quantity="%2$s" class="%3$s" %4$s>%5$s</a>';
return sprintf(
$format,
esc_url( $url ),
esc_attr( $quantity ),
esc_attr( $class ),
$attributes,
esc_html( $text )
);
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'mycode_display_highlighted_add_to_cart_button_for_products_on_sale', 20, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment