Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: WooCommerce Sale Price First
Plugin URI: https://www.damiencarbery.com/
Description: Move the Sale Price to be before the Normal Price in the html but display as Normal-then-Sale. This is because Google Merchant is not reading the sale price.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'woocommerce_format_sale_price', 'dcwd_sale_price_first', 10, 3 );
function dcwd_sale_price_first( $price, $regular_price, $sale_price ) {
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del aria-hidden="true">' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>';
// This CSS should be moved to your stylesheet or Additional CSS in Customizer.
$style = '<style>.single-product div.product p.price { display: flex; } .single-product div.product p.price ins { order: 2; padding-left: 0.5em; }</style>';
return $price . $style;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment