class-cmbird-categories-zi.php
7 months ago
class-cmbird-image-zi.php
5 months ago
class-import-items.php
4 months ago
class-import-price-list.php
9 months ago
class-multi-currency.php
7 months ago
class-order-sync.php
4 months ago
class-product.php
4 months ago
class-users-contact.php
4 months ago
index.php
1 year ago
class-multi-currency.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * All Multi Currency related functions. |
| 5 | * |
| 6 | * @package WooZo Inventory |
| 7 | * @category Zoho Integration |
| 8 | * @author Roadmap Studios |
| 9 | * @link https://commercebird.com |
| 10 | */ |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | class CMBIRD_Multicurrency_Zoho { |
| 16 | |
| 17 | private $config; |
| 18 | public function __construct() { |
| 19 | |
| 20 | $config = array( |
| 21 | |
| 22 | 'MulticurrencyZI' => array( |
| 23 | 'OID' => get_option( 'cmbird_zoho_inventory_oid' ), |
| 24 | 'APIURL' => get_option( 'cmbird_zoho_inventory_url' ), |
| 25 | |
| 26 | ), |
| 27 | |
| 28 | ); |
| 29 | |
| 30 | $this->config = $config; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Retrieve the currency ID for a given user currency. |
| 35 | * |
| 36 | * Execute a cURL call to retrieve all currencies from Zoho Inventory, |
| 37 | * and then loop through the response to find the matching currency code. |
| 38 | * If a match is found, update the user meta with the currency ID and code. |
| 39 | * |
| 40 | * @param string $user_currency The user currency to retrieve the ID for. |
| 41 | * @param int $userid The user ID to update the meta for. |
| 42 | * @param object $order The order object to update the meta for (if applicable). |
| 43 | * |
| 44 | * @return int The currency ID. |
| 45 | */ |
| 46 | public function zoho_currency_data( $user_currency, $userid = '', $order = 0 ) { |
| 47 | $zoho_inventory_oid = $this->config['MulticurrencyZI']['OID']; |
| 48 | $zoho_inventory_url = $this->config['MulticurrencyZI']['APIURL']; |
| 49 | |
| 50 | $cache_key = 'cmbird_zoho_inventory_currencies'; |
| 51 | $currencies = wp_cache_get( $cache_key ); |
| 52 | if ( false === $currencies ) { |
| 53 | $currencies = get_option( $cache_key ); |
| 54 | if ( empty( $currencies ) ) { |
| 55 | $url = $zoho_inventory_url . 'inventory/v1/settings/currencies?organization_id=' . $zoho_inventory_oid; |
| 56 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 57 | $response = $execute_curl_call_handle->execute_curl_call_get( $url ); |
| 58 | if ( isset( $response->code ) && ( 0 === $response->code || '0' === $response->code ) && ! empty( $response->currencies ) ) { |
| 59 | $currencies = $response->currencies; |
| 60 | // filter out unnecessary data before caching. Only store currency_id and currency_code. |
| 61 | $currencies = array_map( |
| 62 | function ( $currency ) { |
| 63 | return (object) array( |
| 64 | 'currency_id' => $currency->currency_id, |
| 65 | 'currency_code' => $currency->currency_code, |
| 66 | ); |
| 67 | }, |
| 68 | $currencies |
| 69 | ); |
| 70 | update_option( $cache_key, $currencies, false ); |
| 71 | } |
| 72 | } |
| 73 | wp_cache_set( $cache_key, $currencies ); |
| 74 | } |
| 75 | |
| 76 | if ( empty( $currencies ) ) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | $currency_id = 0; |
| 81 | foreach ( $currencies as $value ) { |
| 82 | if ( $value->currency_code === $user_currency ) { |
| 83 | $currency_id = $value->currency_id; |
| 84 | if ( '' === $userid ) { |
| 85 | $order->update_meta_data( 'zi_currency_id', $value->currency_id ); |
| 86 | $order->update_meta_data( 'zi_currency_code', $value->currency_code ); |
| 87 | } else { |
| 88 | update_user_meta( $userid, 'zi_currency_id', $value->currency_id ); |
| 89 | update_user_meta( $userid, 'zi_currency_code', $value->currency_code ); |
| 90 | } |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return $currency_id; |
| 96 | } |
| 97 | } |
| 98 |