Skip to content

Instantly share code, notes, and snippets.

@MaryOJob
Forked from andrewlimaza/strike-pmpro-woo.php
Created June 12, 2020 14:40
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 MaryOJob/c6828baf7f379b56d9f04d34601f1e7f to your computer and use it in GitHub Desktop.
Save MaryOJob/c6828baf7f379b56d9f04d34601f1e7f to your computer and use it in GitHub Desktop.
Strike pricing for WooCommerce and Paid Memberships Pro pricing.
<?php
/**
* This will add strike through pricing if the membership pricing is available for currrent user viewing Woo store.
* Add this code (Line 8 onwards) to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprowoo_strike_prices( $price, $product ) {
global $pmprowoo_member_discounts, $current_user;
$level_id = $current_user->membership_level->id;
// get pricing for simple product
if( 'simple' == $product->product_type ) {
// get normal non-member price.
$regular_price = get_post_meta( $product->get_id(), '_regular_price', true );
$sale_price = get_post_meta( $product->get_id(), '_sale_price', true );
// set price variable to sale price if available.
if( !empty( $sale_price ) ) {
$regular_price = $sale_price;
$price = wc_price( pmprowoo_get_membership_price( $regular_price, $product ) );
}
// only show this to members.
if( isset($level_id) && !empty( $pmprowoo_member_discounts ) && !empty( $pmprowoo_member_discounts[ $level_id ] ) ) {
$formatted_price = '<del>' . wc_price( $regular_price ) . '</del> ';
}
$formatted_price .= $price;
// update price variable so we can return it later.
$price = $formatted_price;
}
// get pricing for variable products.
if( 'variable' == $product->product_type ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
$regular_range = wc_format_price_range( $min_price, $max_price );
if( isset($level_id) && !empty( $pmprowoo_member_discounts ) && !empty( $pmprowoo_member_discounts[ $level_id ] ) ) {
$formatted_price = '<del>' . $regular_range . '</del> ';
}
$formatted_price .= $price;
$price = $formatted_price;
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'my_pmprowoo_strike_prices', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment