Last active
October 12, 2024 04:02
-
-
Save webtoffee-git/83034c5c37e2dfa38167ae009058b196 to your computer and use it in GitHub Desktop.
Fix compatibility issue with Curcy Multi Currency plugin on the Gift Card product page - By WebToffee (WebToffee Gift Cards plugin)
This file contains 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 //Do no copy this line of code | |
//Currency converter function | |
if(! function_exists('convert_curcy_price')){ | |
function convert_curcy_price( $price, $selected_currency, $previous_currency='') { | |
if(class_exists('WOOMULTI_CURRENCY_F') && method_exists('WOOMULTI_CURRENCY_F_Data','get_ins')){ | |
$curcy_currency_settings = WOOMULTI_CURRENCY_F_Data::get_ins(); | |
$currency_list = $curcy_currency_settings->get_list_currencies(); | |
$force_convert = false; | |
if( ''!== $previous_currency && $previous_currency !== $selected_currency ){ | |
$force_convert = true; | |
} | |
if( ( $selected_currency !== get_option('woocommerce_currency') && $price > 0 ) || true === $force_convert ) { | |
$woo_currencies = get_woocommerce_currencies(); | |
if( '' !== $previous_currency ){ | |
$price = $price / floatval( $currency_list[ $previous_currency ]['rate'] ); | |
if( $selected_currency === $previous_currency ){ | |
return $price; | |
} | |
} | |
if ( !empty( $woo_currencies[$selected_currency] ) && !empty( $currency_list[ $selected_currency ]['rate'] ) ) { | |
$price = $price * floatval( $currency_list[ $selected_currency ]['rate'] ); | |
} | |
} | |
} | |
return $price; | |
} | |
} | |
// Gift card page predefined amounts | |
add_filter('wt_gc_alter_giftcard_predifined_amounts',function($predefined){ | |
if(class_exists('WOOMULTI_CURRENCY_F') | |
&& method_exists('WOOMULTI_CURRENCY_F_Data', 'get_ins') | |
&& function_exists('convert_curcy_price') | |
){ | |
$curcy_currency_settings = WOOMULTI_CURRENCY_F_Data::get_ins(); | |
$current_currency = $curcy_currency_settings->get_current_currency(); | |
foreach($predefined as $key => $value){ | |
$predefined[$key] = convert_curcy_price($value, $current_currency); | |
} | |
} | |
return $predefined; | |
}); | |
// Cart addition validation | |
add_filter( 'woocommerce_add_cart_item_data', function($cart_item_data, $product_id, $variation_id ){ | |
if(class_exists('Wbte_Gc_Gift_Card_Free_Common') && method_exists('Wbte_Gc_Gift_Card_Free_Common','is_gift_card_product')) | |
{ | |
if(! Wbte_Gc_Gift_Card_Free_Common::is_gift_card_product($product_id)) { | |
return $cart_item_data; | |
} | |
if(isset($cart_item_data['wt_credit_amount']) && class_exists('WOOMULTI_CURRENCY_F')){ | |
$cart_item_data['wt_credit_currency'] = isset($_COOKIE['wmc_current_currency']) ? $_COOKIE['wmc_current_currency']: get_option('woocommerce_currency'); | |
} | |
} | |
return $cart_item_data; | |
}, 11, 3 ); | |
// Cart item Gift card price in cart table | |
add_filter( 'woocommerce_cart_item_price', function($product_price, $cart_item, $cart_item_key ){ | |
if(isset($cart_item['wt_credit_amount']) ){ | |
$product_price = (float) $cart_item['wt_credit_amount']; | |
if ((class_exists('WOOMULTI_CURRENCY_F')) | |
&& method_exists('WOOMULTI_CURRENCY_F_Data', 'get_ins') | |
&& function_exists('convert_curcy_price') | |
&& isset($cart_item['wt_credit_currency'])) { | |
$curcy_currency_settings = WOOMULTI_CURRENCY_F_Data::get_ins(); | |
$current_currency = $curcy_currency_settings->get_current_currency(); | |
if($current_currency !== $cart_item['wt_credit_currency'] || $cart_item['wt_credit_currency'] === get_option('woocommerce_currency')){ | |
$product_price = convert_curcy_price($product_price, $current_currency, $cart_item['wt_credit_currency']); | |
$cart_item['wt_credit_amount'] = $product_price; | |
} | |
} | |
return wp_kses_post( wc_price( $product_price ) ); | |
} | |
return wp_kses_post( wc_price( $product_price ) ); | |
}, 12, 3 ); | |
// Cart item data - subtotal | |
add_action( 'woocommerce_before_calculate_totals', function($cart_obj){ | |
if (is_admin()) { | |
return; | |
} | |
if(class_exists('WOOMULTI_CURRENCY_F') | |
&& method_exists('WOOMULTI_CURRENCY_F_Data', 'get_ins') | |
&& function_exists('convert_curcy_price') | |
) { | |
foreach ($cart_obj->get_cart() as $key => $item) { | |
if (! isset( $item['wt_credit_amount'])) { | |
continue; | |
} | |
$product_price = (float) $item['wt_credit_amount']; | |
$curcy_currency_settings = WOOMULTI_CURRENCY_F_Data::get_ins(); | |
$current_currency = $curcy_currency_settings->get_current_currency(); | |
if($item['wt_credit_currency'] === get_option('woocommerce_currency')){ | |
continue; | |
} | |
$product_price = convert_curcy_price($product_price, $current_currency, $item['wt_credit_currency']); | |
$item['wt_credit_amount'] = $product_price; | |
$item['data']->set_price( $product_price ); | |
} | |
} | |
}, 999, 1 ); | |
// Applied store credit coupon | |
add_action( 'woocommerce_new_order', function($order){ | |
if(class_exists('WOOMULTI_CURRENCY_F') | |
&& class_exists('Wbte_Woocommerce_Gift_Cards_Free_Common') | |
&& method_exists('Wbte_Woocommerce_Gift_Cards_Free_Common','is_store_credit_coupon') | |
&& function_exists('convert_curcy_price') | |
){ | |
$order = ! is_object($order) ? wc_get_order($order) : $order; | |
$cart = WC()->cart; | |
if(empty($cart)) | |
{ | |
return; | |
} | |
$applied_coupons = $cart->get_applied_coupons(); | |
if(empty($applied_coupons)) | |
{ | |
return; | |
} | |
foreach($applied_coupons as $coupon_code) | |
{ | |
$coupon = new WC_Coupon($coupon_code); | |
$coupon_id = $coupon->get_id(); | |
if(!$coupon || !Wbte_Woocommerce_Gift_Cards_Free_Common::is_store_credit_coupon($coupon)) | |
{ | |
continue; | |
} | |
$coupon_amount = get_post_meta($coupon_id, 'coupon_amount', true); | |
if(0 < $coupon_amount){ | |
$order_currency = $order->get_currency(); | |
$coupon_amount = convert_curcy_price($coupon_amount, get_option('woocommerce_currency'), $order_currency); | |
$coupon->set_amount(wc_format_decimal($coupon_amount, 2)); | |
$coupon->save(); | |
} | |
} | |
} | |
}, 11, 1 ); | |
// Newly created store credit coupon | |
add_action( 'woocommerce_new_order_item', function($item_id, $item, $order_id){ | |
if(class_exists('WOOMULTI_CURRENCY_F') | |
&& function_exists('convert_curcy_price')){ | |
$order = wc_get_order($order_id); | |
$order_currency = $order->get_currency(); | |
$legacy_values = $item->legacy_values; | |
if (!isset($legacy_values['wt_credit_amount']) || 0 >= floatval($legacy_values['wt_credit_amount'])) { | |
return; | |
} | |
if(isset($legacy_values['wt_credit_currency']) && $legacy_values['wt_credit_currency'] !== get_option('woocommerce_currency')){ | |
$coupon_datas = wc_get_order_item_meta( $item_id, 'wt_credit_coupon_generated', true ); | |
foreach($coupon_datas as $coupon_data){ | |
$coupon_id = $coupon_data['coupon_id']; | |
$coupon_amount = $coupon_data['credited_amount']; | |
$coupon_obj = new WC_Coupon( $coupon_id ); | |
$coupon_updated_amount = convert_curcy_price($coupon_amount,$order_currency, $legacy_values['wt_credit_currency']); | |
$coupon_obj->set_amount(wc_format_decimal($coupon_updated_amount,2)); | |
$coupon_obj->save(); | |
} | |
} | |
} | |
}, 999, 3 ); | |
/** | |
* Convert currency in Order edit page , emails and PDFs according to order currency | |
*/ | |
add_filter('wbte_gc_alter_prices_on_giftcard',function($price,$args){ | |
if(class_exists('WOOMULTI_CURRENCY_F') | |
&& method_exists('WOOMULTI_CURRENCY_F_Data','get_ins') | |
&& function_exists('convert_curcy_price') | |
){ | |
if( $args['order'] ){ | |
$order_currency = $args['order']->get_currency(); | |
$curcy_currency_settings = WOOMULTI_CURRENCY_F_Data::get_ins(); | |
$currency_list = $curcy_currency_settings->get_list_currencies(); //get list currency | |
$order_currency_rate = floatval( $currency_list[ $order_currency ]['rate'] ); | |
if( $order_currency_rate ){ | |
$coupon_amount = get_post_meta( $args['coupon']->get_id(), 'coupon_amount' )[0]; | |
$amount = $coupon_amount * $order_currency_rate; | |
return wc_price( $amount, array( 'currency' => $order_currency ) ); | |
} | |
} | |
} | |
return $price; | |
}, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment