Last active
May 8, 2024 16:19
-
-
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.
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
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 = (float) WC()->cart->get_total( 0 ); // Set context to 0 because we just need the raw value. | |
$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