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;
}
@tremblayly
Copy link

Can the display both prices in the cart and at checkout pages be adapted to displaying the wholesale price based on wholesale user role?

@damiencarbery
Copy link
Author

damiencarbery commented Jun 21, 2019

Can the display both prices in the cart and at checkout pages be adapted to displaying the wholesale price based on wholesale user role?

Probably (I don't 100% understand your question but I am sure that displaying the full and wholesale prices could be displayed in the cart and checkout).

In the dcwd_sale_price() function you would need to add extra checks e.g. that the user is logged in and find their role.
Are you using a plugin to set the wholesale price and the wholesale user role? How many different wholesale user roles do you have?
What price is currently being displayed? Full or wholesale?

@tremblayly
Copy link

I use the Wholesale Suite of plugins - Wholesale Premium Prices which allows me to create the roles and enter pricing for each of them within when creating a product. I have 15 wholesale roles because of tax exemptions (in Canada we have two taxes (GST & PST) and some customers can exempted from paying one only or none):
// charge both PST and GST $map['wholesale_customer'] = 'WholesalePSTGST'; $map['ws_silvia_silver'] = 'WholesalePSTGST'; $map['ws_silvia_gold'] = 'WholesalePSTGST'; $map['ws_silvia_premium'] = 'WholesalePSTGST'; $map['ws_silvia_union'] = 'WholesalePSTGST'; // charge only GST $map['wholesale_pst_exempt'] = 'WholesalePSTExempt'; $map['ws_silvia_silver_pst_exempt'] = 'WholesalePSTExempt'; $map['ws_silvia_gold_pst_exempt'] = 'WholesalePSTExempt'; $map['ws_silvia_premium_pst_exempt'] = 'WholesalePSTExempt'; $map['ws_silvia_union_pst_exempt'] = 'WholesalePSTExempt'; // charge neither $map['wholesale_tax_exempt'] = 'WholesaleZeroTax'; $map['ws_silvia_silver_tax_exempt'] = 'WholesaleZeroTax'; $map['ws_silvia_gold_tax_exempt'] = 'WholesaleZeroTax'; $map['ws_silvia_premium_tax_exempt'] = 'WholesaleZeroTax'; $map['ws_silvia_union_tax_exempt'] = 'WholesaleZeroTax';

The shop and single product pages, once a wholesale customer logs in, he can see the retail (with a line across) and the wholesale prices. However in the cart and checkout pages, the customer only sees the wholesale prices applied. Customers have been asking to see both prices and my client also wants both prices to show - including subtotals/totals.

@tremblayly
Copy link

That code snippet is for applying different tax rates to specific wholesale user roles.

@damiencarbery
Copy link
Author

damiencarbery commented Jun 21, 2019

Is it this plugin? https://wholesalesuiteplugin.com/woocommerce-wholesale-prices-premium/
It's a premium plugin that I do not have access to.
You could look to find what code sets the display price in cart and checkout - it's probably an apply_filters() call that you could add_filter() to. It's impossible to give any other ideas without seeing the plugin code.

As the plugin is a premium one, you should ask their Support people to help you - that is part of the thing you are paying for.

@tremblayly
Copy link

tremblayly commented Jun 21, 2019 via email

@damiencarbery
Copy link
Author

I didn't see any zip attached. Email me directly at damien@damiencarbery.com

@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