IN WooCommerce I am using the code of this tread to display with a short code the product prices from a defined product ID. But it don\'t really do what I want. Here is that
@LoicTheAztec thanks so much for your code!!!
I've done done a few modifications, so I'm sharing it here. Maybe it's useful for somebody too :)
Note: this version is NOT "plug and play". In other words: it needs some work if you want to adapt it to your needs.
This version also changes the currency depending on the page language. For example:
Note: this version also:
Code:
/**
* Shortcode to display default price and discounted price
* USAGE:
* [product_price id="37"]
* Original code: https://stackoverflow.com/a/47237674/1198404
* Modificated code to fit our needs: https://stackoverflow.com/a/63418231/1198404
*/
function custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Build checkout URL like: https://www.edinventa.com/fr/checkout/?fill_cart=7982&
$domain_base_url = get_bloginfo('wpurl');
$wpml_language_code = ICL_LANGUAGE_CODE;
$checkout_url = $domain_base_url . "/" . $wpml_language_code . "/checkout/?fill_cart=" . $atts['id'];
// Get currency symbol
$currency_symbol = get_woocommerce_currency();
// Your price CSS styles
$style1 = 'class="my-discount-price" style="outline: none; text-decoration: none;"';
$style2 = 'class="my-standard-price" style="text-decoration: line-through;"';
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'currency' => '',
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals' => wc_get_price_decimals(),
'price_format' => get_woocommerce_price_format(),
);
// Formatting html output with discount price with link like: 49€
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "" . wc_price( $regular_price, $args ) . " " . wc_price( $sale_price, $args ) . ""; // Sale price is set
else
$html = "" . wc_price( $sale_price, $args ) . "€"; // No sale price set (UNTESTED)
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
CSS code:
.my-standard-price{
font-size:200%;
}
.my-discount-price{
font-size:150%;
}
Result: