Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created January 4, 2016 00:48
Show Gist options
  • Save bekarice/21f67d2a1eda138fe6f1 to your computer and use it in GitHub Desktop.
Save bekarice/21f67d2a1eda138fe6f1 to your computer and use it in GitHub Desktop.
Change WooCommerce price display with a custom field
<?php
// only copy the opening php tag if needed
// Change the shop / product prices if a unit_price is set
function sv_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'unit_price', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . ' per kg</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_html', 10, 2 );
// Change the cart prices if a unit_price is set
function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
$unit_price = get_post_meta( $cart_item['product_id'], 'unit_price', true );
if ( ! empty( $unit_price ) ) {
$price = wc_price( $unit_price ) . ' per kg';
}
return $price;
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
@alvinarichard
Copy link

I am trying to change the WooCommerce pricing but having an issues. I have added this code in functions.php

function cw_change_product_price_display( $price ) {
    $price .= ' At Each Item Product';
    return $price;
  }
  add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
  add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

I have seen this code here https://www.cloudways.com/blog/change-woocommerce-price-display/ and implemented the same steps.

@devomman
Copy link

devomman commented Apr 13, 2020

Hi, I want add this to Checkout Page and all other Page i mean Email, Invoice How To use.

@Vorevex
Copy link

Vorevex commented Oct 19, 2020

Hi, I have implemented the function in my functions.php file, but now I have a question and it is that when I make a discount, the $ unit_price does not change. The developers of the plugin have told me that it can be done with the following function:
WC_Product :: get_sale_price function
Here is the reference: https://woocommerce.wp-a2z.org/oik_api/wc_productget_sale_price/

The fact is that I know little about programming, if you can help me, I would appreciate it.

Greetings.

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