Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 6, 2021 18:47
Show Gist options
  • Save jorpdesigns/13e0ad993c5936a36c612f9ad3847458 to your computer and use it in GitHub Desktop.
Save jorpdesigns/13e0ad993c5936a36c612f9ad3847458 to your computer and use it in GitHub Desktop.
Snippet to modify WooCommerce product price display
<?php
add_filter( 'woocommerce_get_price_html', 'custom_price_display', 10, 2 );
function custom_price_display( $price, $product ) {
// CHANGE FOR ALL PRODUCTS
$price .= ' for each item';
// FOR SPECIFIC PRODUCT
if ( $product->get_id() == 15 ) { // Replace 15 with your product ID
$price .= ' for this particular product';
}
// ADD CUSTOM PRICE FROM META e.g. Unit Price
// $unitPrice = get_field('unit_price', $product->get_id()); // Uncomment if custom field was added by ACF
// $unitPrice = get_post_meta( $product->get_id(), 'unit_price', true ); // Uncomment if custom field was not added by ACF
if ( ! empty( $unitPrice ) ) {
$price .= '<span class="unit-price">' . wc_price( $unitPrice ) . ' per unit</span>';
}
return $price;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment