Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active July 10, 2021 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/757308c10af2ca103330b4b4673e6a49 to your computer and use it in GitHub Desktop.
Save damiencarbery/757308c10af2ca103330b4b4673e6a49 to your computer and use it in GitHub Desktop.
<span class="price">
<del><span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">&euro;</span>65.00
</span></del>
<ins><span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">&euro;</span>55.00
</span></ins>
</span>
<?php
/*
Plugin Name: Show Sale Price and RRP - per product or category
Plugin URI: https://www.damiencarbery.com/2018/04/woocommerce-display-only-sale-price/
Description: Display discounted or sale price with the RRP under it. Limit to specified products and categories.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.2
*/
add_filter( 'woocommerce_format_sale_price', 'dcwd_sale_price', 20, 3 );
function dcwd_sale_price( $price, $regular_price, $sale_price ) {
// Display regular price when viewing these products or categories.
$products = array( 27, 28 );
$categories = array( 'accessories', 'hoodies' );
$show_regular_price = false;
// Include products in a category archive and in related products section.
$product_id = get_the_ID();
if ( in_array( $product_id, $products ) ) {
$show_regular_price = true;
}
// Check whether current category is in the list.
if ( is_product_category() ) {
$queried_object = get_queried_object();
$category_slug = $queried_object->slug;
if ( in_array( $category_slug, $categories ) ) {
$show_regular_price = true;
}
}
if ( $show_regular_price ) {
return sprintf( '<span class="sale_price">%s</span> <span class="rrp">RRP: %s</span>',
wc_price( $sale_price ), wc_price( $regular_price ) );
}
else {
return $price;
}
}
<?php
/*
Plugin Name: Show Sale Price and RRP
Plugin URI: https://www.damiencarbery.com/2018/04/woocommerce-display-only-sale-price/
Description: Display discounted or sale price with the RRP under it.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'woocommerce_format_sale_price', 'dcwd_sale_price', 20, 3 );
function dcwd_sale_price( $price, $regular_price, $sale_price ) {
return sprintf( '<span class="sale_price">%s</span> <span class="rrp">RRP: %s</span>',
wc_price( $sale_price ), wc_price( $regular_price ) );
}
<span class="price">
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">&euro;</span>55.00
</span>
</span>
<?php
/*
Plugin Name: Show Only Sale Price (variable products)
Plugin URI: https://www.damiencarbery.com/2018/04/woocommerce-display-only-sale-price/
Description: Display only the reduced price when a product is discounted or on sale.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'woocommerce_variable_price_html', 'dcwd_variable_price', 10, 2 );
function dcwd_variable_price( $price_html, $product ) {
if ( $product->is_on_sale() ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$price = wc_price( $min_price );
return 'From: ' . $price;
}
return $price_html;
}
<?php
/*
Plugin Name: Show Only Sale Price
Plugin URI: https://www.damiencarbery.com/2018/04/woocommerce-display-only-sale-price/
Description: Display only the reduced price when a product is discounted or on sale.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.2
*/
// For simple products.
add_filter( 'woocommerce_format_sale_price', 'dcwd_sale_price', 20, 3 );
function dcwd_sale_price( $price, $regular_price, $sale_price ) {
/*global $product;
// Show regular and sale price for specified product IDs
if ( in_array( $product->get_id(), array( 1, 2, 3, 28 ) ) ) {
return $price;
}
// Show regular and sale price if product is in any of the specified categories.
if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product->get_id() ) ) {
return $price;
}*/
return wc_price( $sale_price );
}
// For variable products.
add_filter( 'woocommerce_variable_price_html', 'dcwd_variable_price', 10, 2 );
function dcwd_variable_price( $price_html, $product ) {
if ( $product->is_on_sale() ) {
/*// Show regular and sale price for specified product IDs
if ( in_array( $product->get_id(), array( 1, 2, 3, 7932 ) ) ) {
return $price_html;
}
// Show regular and sale price if product is in any of the specified categories.
if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product->get_id() ) ) {
return $price_html;
}*/
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$price = wc_price( $min_price );
return 'From: ' . $price;
}
return $price_html;
}
@damiencarbery
Copy link
Author

For anyone else interested int the code that I wrote for Lyse:
Show regular price in cart and checkout when using WooCommerce Wholesale Prices
https://www.damiencarbery.com/2019/07/show-regular-price-in-cart-and-checkout-when-using-woocommerce-wholesale-prices/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment