Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Created September 19, 2023 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UVLabs/fcb049cb0245c9c8dc807af3c9af4db6 to your computer and use it in GitHub Desktop.
Save UVLabs/fcb049cb0245c9c8dc807af3c9af4db6 to your computer and use it in GitHub Desktop.
Adds the XCD equivalent of a product's price next to the USD amount. The example is for XCD but can be used for any currency by just changing the conversion rate.
function sl_add_price_suffix( $html, $product, $price, $qty ){
if(empty($product) && !is_object($product)){
return;
}
$is_variable = $product->is_type('variable');
$min_price = '';
$max_price = '';
if( $is_variable ){
$min_price = $product->get_variation_regular_price('min');
$max_price = $product->get_variation_regular_price('max');
}
if( $is_variable ){
$html .= " USD" . " <span class='ec-equiv' style='font-size: 12px; font-weight: bold'>($" . number_format( ($min_price / 0.37), 2 ) . " EC - $" . number_format( ($max_price / 0.37), 2 ) . " EC)</span>";
}else{
$html .= " USD" . " <span class='ec-equiv' style='font-size: 12px; font-weight: bold'>($" . number_format( ($product->get_price() / 0.37), 2 ) . " EC)</span>";
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'sl_add_price_suffix', 99, 4 );
/**
* Show the customer the amount they'd be paying in USD.
*
* @return void
*/
function sl_show_usd_total_at_checkout() : void {
$total = WC()->cart->total;
$total = $total / 0.37; // Change conversion rate as necessary
$total = number_format( $total, 2, '.', ',' );
$markup = <<<HTML
<div>
<p style="text-align: center; font-weight: bold; font-size: 20px"><span style="color: red;">**</span> Total in EC: $$total <span style="color: red;">**</span><br/>
<ul>
<li><small><strong>NOTE:</strong> You will be billed in USD.</small></li>
<li><small><strong>NOTE:</strong> EC price shown is an estimate based on the standard USD-EC conversion rate.</small></li>
<ul>
</p>
</div>
HTML;
echo $markup;
}
add_action( 'woocommerce_review_order_before_payment', 'sl_show_usd_total_at_checkout' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment