Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created September 20, 2017 12:59
Show Gist options
  • Save xlplugins/b605c300866ffcdf40e82f8f026a8bc0 to your computer and use it in GitHub Desktop.
Save xlplugins/b605c300866ffcdf40e82f8f026a8bc0 to your computer and use it in GitHub Desktop.
Customizing 'woocommerce_product_addons_option_price' filter hook for Fabio Tielen
<?php
/**
* Add below function in theme functions.php file
*/
add_filter('woocommerce_product_addons_option_price', 'theme_name_product_addons_show_msrp', 20, 4);
function theme_name_product_addons_show_msrp($html, $option, $idx, $type) {
// User has chosen not to show MSRP, don't show it
$msrp_status = get_option('woocommerce_msrp_status');
if (empty($msrp_status) || 'never' === $msrp_status) {
return $html;
}
// Get the info we need.
$template = ''; // initialzing template variable with blank value to avoid notices
if (is_user_logged_in()) { // if user logged in
$template = __(
'(%1$s, <span class="woocommerce_msrp">%2$s: <span class="woocommerce_msrp_price">%3$s</span></span>)', 'woocommerce_msrp'
);
}
if ('select' === $type) {
$template = __(
'(%1$s, %2$s: %3$s)', 'woocommerce_msrp'
);
}
$raw_price = isset($option['price']) ? $option['price'] : null;
$raw_msrp = isset($option['msrp']) ? $option['msrp'] : null;
if (empty($raw_msrp)) {
return $html;
}
$price_html = $option['price'] > 0 ? wc_price(get_product_addon_price_for_display($option['price'])) : '';
$msrp_description = get_option('woocommerce_msrp_description');
$msrp_price_html = !empty($option['msrp']) ? wc_price($option['msrp']) : '';
// Check whether we should show the MSRP, and either return the original markup
// if not, or the MSRP included markup.
if ('different' === $msrp_status && $raw_price === $raw_msrp) {
return $html;
}
return sprintf($template, $price_html, $msrp_description, $msrp_price_html);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment