Created
January 4, 2016 00:48
-
-
Save bekarice/21f67d2a1eda138fe6f1 to your computer and use it in GitHub Desktop.
Change WooCommerce price display with a custom field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
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
I am trying to change the WooCommerce pricing but having an issues. I have added this code in functions.php
I have seen this code here https://www.cloudways.com/blog/change-woocommerce-price-display/ and implemented the same steps.