1. Home
  2. Currency per Product for WooCommerce
  3. Hooks: Action and Filter Reference

Hooks: Action and Filter Reference

This document provides a comprehensive list of all hooks available within the plugin. It serves as a resource for developers aiming to enhance the plugin’s functionality.

How can I avoid currency to convert to shop’s default currency on the Cart page?

/**
* Setting converted price on the checkout page.
*
* @param Object $cart_obj Cart Object.
*/
function alg_wc_cpp_set_converted_price( $cart_obj ) {

if ( is_checkout() || is_page( 'checkout' ) ) { // This is necessary for WC 3.0+

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

if ( has_filter( 'alg_wc_cpp_convert_shop_default' ) ) {
foreach ( $cart_obj->get_cart() as $cart_item ) {

$do_save_prices = ( 'yes' === get_option( 'alg_wc_cpp_save_products_prices', 'no' ) );

if ( $do_save_prices ) {
$product_id = alg_wc_cpp_get_product_id( $cart_item['product_id'] );
}

if ( isset( $cart_item['data']->alg_wc_cpp ) ) {
// Behaviour options.
$convert_in_shop = ( 'convert_shop_default' === get_option( 'alg_wc_cpp_shop_behaviour', 'show_in_different' ) );

$cart_checkout_behaviour = ( $convert_in_shop ? 'convert_shop_default' : get_option( 'alg_wc_cpp_cart_checkout', 'convert_shop_default' ) );

if ( 'convert_shop_default' === $cart_checkout_behaviour ) {
$exchange_rate = alg_wc_cpp_get_currency_exchange_rate( $cart_item['data']->alg_wc_cpp );
$return_price = $cart_item['data']->get_price() * $exchange_rate;

if ( 'yes' === get_option( 'alg_wc_cpp_round_off_decimal_points' ) ) {
$return_price = round( $return_price, 1 );
}
$cart_item['data']->set_price( $return_price );
}
}
}
}
}
}

add_action( 'woocommerce_before_calculate_totals', 'alg_wc_cpp_set_converted_price', 20, 1 );

/**
* Return false so that Product Price will be return.
*
* @param bool $flag false to return product price.
*/
function alg_wc_cpp_convert_shop_default( $flag, $alg_wc_cpp_core_class ) {
return false;
}

add_filter( 'alg_wc_cpp_convert_shop_default', 'alg_wc_cpp_convert_shop_default', 10, 2 );

/**
* Set Product Currency on cart page.
*
* @param bool $flag Return Product Currency.
*/
function alg_wc_cpp_get_currency_convert_shop_default( $flag ) {

if ( is_cart() ) {
// if on cart page then
$cart_items = WC()->cart->get_cart();

foreach ( $cart_items as $cart_item ) {
return ( isset( $cart_item['alg_wc_cpp'] ) ) ? $cart_item['alg_wc_cpp'] : false;
}
}
return $flag;
}

add_filter( 'alg_wc_cpp_get_currency_convert_shop_default', 'alg_wc_cpp_get_currency_convert_shop_default', 10, 1 );

/**
* Get Order Currency on edit order page.
*
* @param bool $status Status.
* @param mixed $product_id Product ID.
*/
function alg_wc_cpp_get_order_currency_callback( $status, $product_id ) {

if ( 'shop_order' === get_post_type( $product_id ) ) {
// Get the order currency.
$currency = get_post_meta( $product_id, '_order_currency', true );

if ( empty( $currency ) ) {
// Check if this order is part of a subscription.
$subscription_id = get_post_meta( $product_id, '_subscription_renewal', true );
// Find the currency of the parent subscription order.

if ( ! empty( $subscription_id ) ) {
$currency = get_post_meta( $subscription_id, '_order_currency', true );
}
}
return ( '' !== $currency ) ? $currency : $status;
}
return $status;
}

add_filter( 'alg_wc_cpp_get_order_currency', 'alg_wc_cpp_get_order_currency_callback', 10, 2 );

/**
* Rounded whole price or rounded only decimal value.
*
* @param bool $flag Return true or false.
*/
function alg_wc_cpp_rounded_whole_price_callback( $flag ) {

$flag = true;
return $flag;
}

add_filter( 'alg_wc_cpp_rounded_whole_price', 'alg_wc_cpp_rounded_whole_price_callback', 10, 1 );
Was this article helpful to you? Yes No

How can we help?