Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
6194ba0
Force the currency to CAD
Mar 26, 2021
48d497a
Force the currency on the order
Mar 26, 2021
29bd314
Remove the force on the order currency
Mar 26, 2021
abdfa95
Merge branch 'main' into dev/cory/add-multicurrency-support
Mar 26, 2021
1cf0013
Merge branch 'dev/cory/reorganize-rename-files' into dev/cory/add-mul…
Mar 26, 2021
286de2c
Add support for updating orders for multicurrency
Mar 26, 2021
e52fa62
Include the multicurrency file
Mar 26, 2021
18b501b
Add code comment
Mar 26, 2021
b02b31d
Compare the WC currency to the order currency
Mar 26, 2021
e9f3348
Merge branch 'main' into dev/cory/add-multicurrency-support
Mar 26, 2021
d275202
Merge branch 'main' into dev/cory/add-multicurrency-support
Mar 28, 2021
374ace8
Add currency attribute to buttons
Mar 30, 2021
26e3c01
Hard code list of built-in supported plugins
Mar 30, 2021
efe8d2e
Update number of variables on filter
Mar 30, 2021
7261302
Calculate new price on order products
Mar 30, 2021
77f4c87
Update multicurrency plugin support
Mar 30, 2021
490a8e2
Merge branch 'main' into dev/cory/add-multicurrency-support
Mar 31, 2021
2d09571
Update prefix after merge
Mar 31, 2021
7d10ea2
Update prefix
Mar 31, 2021
bd3627d
Update prefix after merge
Mar 31, 2021
ac78d2c
Update prefixes
Mar 31, 2021
1cfa3a1
Return price on currency integrations
Apr 1, 2021
140f3fa
Fix typo
Apr 1, 2021
1d77316
Add support for currency switcher
Apr 1, 2021
34b19ae
Fix incorrect function name is_empty to empty
Apr 1, 2021
aa0fb8c
Fix typo
Apr 1, 2021
c121a82
Include the name of the multicurrency plugin
Apr 1, 2021
271dd29
Add more detailed comment to apply filters
Apr 1, 2021
488e3be
Update order of passed variables
Apr 1, 2021
c775d5a
Update call for currency switcher to get new price
Apr 1, 2021
a1a7d42
Merge branch 'main' into dev/cory/add-multicurrency-support
Apr 8, 2021
57938ac
Set WooCommerce currency before getting shipping
Apr 11, 2021
97e06e8
Use global for fast currency
Apr 11, 2021
6b319ce
Remove currency update code
Apr 11, 2021
b50d9b3
Trigger multicurrency filter for shipping rates
Apr 11, 2021
5573a83
Add shipping filters to multicurrency drivers
Apr 12, 2021
58a5198
Merge branch 'main' into dev/cory/add-multicurrency-support
Apr 16, 2021
e68b1e2
Update multicurrency functions
Apr 16, 2021
40fffa2
Add error logs
Apr 22, 2021
6d51904
Add more error logs
Apr 22, 2021
330d3de
Add base route class
Apr 22, 2021
e812e03
Refactor shipping route
Apr 22, 2021
6a9faf5
Get currency from request
Apr 22, 2021
ffd1db3
Make calls to WC consistent
Apr 22, 2021
f6f9b5a
Update how shipping taxes are calculated
Apr 22, 2021
d8cdae1
Update shipping tax calculation
Apr 22, 2021
53e487b
Remove error_log calls
Apr 22, 2021
9df6d93
Merge branch 'dev/cory/add-multicurrency-support' into dev/cory/refac…
Apr 23, 2021
d602b84
Rename base class file
Apr 23, 2021
82b739c
Update shipping route
Apr 23, 2021
befa44b
Rename shipping route class
Apr 23, 2021
93e494f
Rename shiping zones and plugin info classes
Apr 23, 2021
f6ea025
Rename base class file
Apr 23, 2021
4bf00a7
Refactor plugin info and shipping zones routes
Apr 23, 2021
e1a0b52
Update calls to routes
Apr 23, 2021
6803b63
Add type hint
Apr 23, 2021
96033fb
Add request object to multicurrency shipping filter
Apr 23, 2021
e1fa153
Load route base class
Apr 23, 2021
a10d5ba
Fix undefined variable
Apr 23, 2021
217837d
Move the setting of the WC currency
Apr 23, 2021
785d32e
Merge branch 'dev/cory/add-multicurrency-support' into dev/cory/refac…
Apr 23, 2021
ae3015f
Update how the wc currency is set
Apr 23, 2021
8d32008
Delete old shipping route file
Apr 23, 2021
1a11fe5
Remove type hint
Apr 28, 2021
d11fe03
Update how route namespaces are set
Apr 28, 2021
3c3981d
Update how route params are set
Apr 28, 2021
7f173e9
Update shipping route method to POST
Apr 28, 2021
6ebc0cf
Update route objects
Apr 28, 2021
4854e34
Remove unused method
Apr 28, 2021
bf557e0
Update how currency is set
Apr 28, 2021
5fcb7a7
Update WCPBC support
Apr 28, 2021
6caa06d
Add setting to disable multicurrency
Apr 29, 2021
f621b74
Add billing info to customer on shipping endpoint
Apr 29, 2021
728c39f
Update support for WCPBC multicurrency plugin
Apr 29, 2021
deb8a63
Hide currency attribute if multicurrency disabled
Apr 29, 2021
80da6c8
Pass product ID instead of product object
Apr 29, 2021
ea5ed9e
Get the currency from the order
May 4, 2021
b0fbcfe
Update the get_currency method
May 6, 2021
88f7b5b
Update classes to use base class parameters
May 6, 2021
05c0484
Fix typo
May 6, 2021
d9d8600
Check for code before comparing
May 6, 2021
a033d7e
Set based on address for wcpbc plugin
May 6, 2021
cf3f4fe
Fix namespace issue
May 6, 2021
265575f
Merge branch 'main' into dev/cory/add-multicurrency-support
May 11, 2021
e60eacd
Merge branch 'main' into dev/cory/add-multicurrency-support
May 14, 2021
21aad4d
Merge branch 'main' into dev/cory/add-multicurrency-support
May 14, 2021
e81d1cf
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 2, 2021
645d787
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 7, 2021
2cbf00d
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 8, 2021
ff71c14
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 9, 2021
1556c42
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 9, 2021
a191222
Fix number of params for filter
Jun 10, 2021
0caf3cc
Fix emtpy typo
Jun 10, 2021
22f9074
Add debug info to multicurrency script
Jun 10, 2021
7b8f874
Removed product from debug
Jun 11, 2021
215ec69
Remove need to check for active mc plugin
Jun 11, 2021
e32b4b4
Log cases where the country value is empty
Jun 11, 2021
b91a447
Update to get zone by country or currency
Jun 14, 2021
0f7cf43
Simplify foreach loop
Jun 14, 2021
3a98824
Reduce the number of returns
Jun 14, 2021
49a606e
Get zone by currency if currencies don’t match
Jun 14, 2021
105dce6
Use correct method
Jun 14, 2021
fadcffd
Add base class for multicurrency support
Jun 22, 2021
667031d
Replace currency switcher with class
Jun 22, 2021
299c923
Rename woocommerce currency switcher
Jun 22, 2021
762ec69
Convert WOOCS to use class
Jun 22, 2021
6586215
Rename WooCommerce Price Based on Countries
Jun 22, 2021
fc108b9
Update WC Price Based on Countries to use class
Jun 22, 2021
a8bd347
Change how mc classes are loaded
Jun 22, 2021
4540063
Add filter to update the order
Jun 22, 2021
4472e93
Add handlers for filter to update order
Jun 22, 2021
581306d
Add root namespace where needed
Jun 22, 2021
8eeb2e6
Add root namespace
Jun 22, 2021
7029786
Update the order currency if needed
Jun 22, 2021
8a62194
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 23, 2021
7b78d71
Merge branch 'main' into dev/cory/add-multicurrency-support
Jun 28, 2021
c206227
Update after merge
Jun 28, 2021
762512b
Merge branch 'main' into dev/cory/add-multicurrency-support
Jul 1, 2021
53fb1b0
Merge branch 'main' into dev/cory/add-multicurrency-support
Jul 8, 2021
fccf047
Update after merge
Jul 8, 2021
7d1c8ba
Merge branch 'main' into dev/cory/add-multicurrency-support
Jul 9, 2021
5a5db4f
Merge branch 'main' into dev/cory/add-multicurrency-support
Aug 19, 2021
203b284
Update routes file after merge
Aug 19, 2021
c119938
Update button template after merge
Aug 19, 2021
d0b259d
Rename functions
Aug 25, 2021
a277043
Update get_currency method
Aug 26, 2021
ca7e6db
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 1, 2021
f02e9e5
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 2, 2021
cd46550
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 7, 2021
d7fe4c2
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 24, 2021
2f2e03c
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 27, 2021
6614616
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 27, 2021
3da5f57
Merge branch 'main' into dev/cory/add-multicurrency-support
Sep 29, 2021
dd11e55
Merge branch 'main' into dev/cory/add-multicurrency-support
Oct 11, 2021
20122ca
Merge branch 'main' into dev/cory/add-multicurrency-support
Oct 11, 2021
78495aa
Merge branch 'main' into dev/cory/add-multicurrency-support
Oct 11, 2021
07cd7a3
Merge branch 'main' into dev/cory/add-multicurrency-support
Oct 15, 2021
fbf53b7
Merge branch 'main' into dev/cory/add-multicurrency-support
Oct 26, 2021
a70f965
Update after merge
Oct 26, 2021
0a9a753
Merge branch 'main' into dev/cory/add-multicurrency-support
Nov 4, 2021
6939a14
Merge branch 'main' into dev/cory/add-multicurrency-support
Nov 4, 2021
9a8346f
Merge branch 'main' into dev/cory/add-multicurrency-support
Dec 8, 2021
8d200c1
Merge branch 'main' into dev/cory/add-multicurrency-support
Dec 13, 2021
9fbdb21
Merge branch 'main' into dev/cory/add-multicurrency-support
Dec 13, 2021
cc33a11
Merge branch 'main' into dev/cory/add-multicurrency-support
Dec 16, 2021
61b80d8
Merge branch 'main' into dev/cory/add-multicurrency-support
Dec 16, 2021
1d05828
Merge branch 'main' into dev/cory/add-multicurrency-support
Feb 9, 2022
f28e3df
Merge branch 'main' into dev/cory/add-multicurrency-support
Mar 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fast.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
require_once FASTWC_PATH . 'includes/assets.php';
// Loads fast utilities.
require_once FASTWC_PATH . 'includes/utilities.php';
// Add suppot for multicurrency.
require_once FASTWC_PATH . 'includes/multicurrency.php';
// Adds Fast Checkout button to store.
require_once FASTWC_PATH . 'includes/checkout.php';
// Adds Fast Login button to store.
Expand Down
6 changes: 5 additions & 1 deletion includes/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ function fastwc_woocommerce_before_checkout_form() {
*
* @param WC_Data $order Object object.
* @param WP_REST_Request $request Request object.
*
* @return WC_Data
*/
function fastwc_woocommerce_rest_pre_insert_shop_order_object( $order, $request ) {

fastwc_log_debug( 'Request object: ' . print_r( $request, true ) ); // phpcs:ignore
$order = fastwc_maybe_update_order_for_multicurrency( $order, $request );

fastwc_log_debug( 'Request object: ' . print_r( $request, true ) );

fastwc_log_debug( 'fastwc_woocommerce_rest_pre_insert_shop_order_object ' . print_r( $order, true ) ); // phpcs:ignore

Expand Down
173 changes: 173 additions & 0 deletions includes/multicurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Add support for multicurrency functionality.
*
* @package fast
*/

// Add base multicurrency class.
require_once FASTWC_PATH . 'includes/multicurrency/class-base.php';
// Add support for Currency Switcher for WooCommerce plugin.
require_once FASTWC_PATH . 'includes/multicurrency/class-currency-switcher-woocommerce.php';
// Add support for WooCommerce Currency Switcher plugin.
require_once FASTWC_PATH . 'includes/multicurrency/class-woocommerce-currency-switcher.php';
// Add support for Price Based on Country for WooCommerce plugin.
require_once FASTWC_PATH . 'includes/multicurrency/class-woocommerce-product-price-based-on-countries.php';

/**
* Checks if multicurrency support is disabled.
*
* @return bool True if multicurrency support is disabled. False otherwise.
*/
function fastwc_is_multicurrency_support_disabled() {
$fastwc_disable_multicurrency = get_option( FASTWC_SETTING_DISABLE_MULTICURRENCY, false );

if ( ! empty( $fastwc_disable_multicurrency ) ) {
return true;
}

return false;

}

/**
* Maybe update order for multicurrency.
*
* @param WC_Data $order The order to check.
* @param WP_REST_Request $request Request object.
*
* @return WC_Data
*/
function fastwc_maybe_update_order_for_multicurrency( $order, $request ) {
$multicurrency_disabled = fastwc_is_multicurrency_support_disabled();

// Do nothing if multicurrency is disabled.
if ( false === $multicurrency_disabled ) {
$wc_currency = get_woocommerce_currency();
$order_currency = fastwc_get_order_currency( $order );

if (
! empty( $order_currency ) // Make sure the order currency is set.
&& $wc_currency !== $order_currency // Make sure the order currency is not the default currency.
) {
$order = fastwc_update_order_for_multicurrency( $order, $request );
}
}

return $order;
}

/**
* Get the order currency.
*
* @param WC_Data $order The order to check.
*
* @return string
*/
function fastwc_get_order_currency( $order ) {
$wc_currency = get_woocommerce_currency();

return method_exists( $order, 'get_currency' ) ? $order->get_currency() : $wc_currency;
}

/**
* Update the order for multicurrency.
*
* @param WC_Data $order The order to check.
* @param WP_REST_Request $request Request object.
*
* @return WC_Data
*/
function fastwc_update_order_for_multicurrency( $order, $request ) {

/**
* Maybe update the order from the multicurrency plugin.
*
* @param WC_Data $order The order to check.
* @param WP_REST_Request $request Request object.
*/
$order = apply_filters(
'fastwc_update_order_for_multicurrency',
$order,
$request
);

foreach ( $order->get_items() as $item_id => $item ) {
$product = method_exists( $item, 'get_product' ) ? $item->get_product() : null;
$quantity = method_exists( $item, 'get_quantity' ) ? (int) $item->get_quantity() : 0;

if ( ! empty( $product ) ) {
/**
* Get the price from the multicurrency plugin.
*
* @param string $price Value of the price.
* @param WC_Product $product The product object.
* @param WC_Data $order The order to check.
* @param WC_Request $request Request object.
*/
$price = apply_filters(
'fastwc_update_price_for_multicurrency',
$product->get_price(),
$product,
$order,
$request
);

// Calculate the price by multiplying by quantity.
$new_price = $price * $quantity;

// Set the new price.
$item->set_subtotal( $new_price );
$item->set_total( $new_price );

// Make new tax calculations.
$item->calculate_taxes();

// Save the item data.
$item->save();
}
}

$order->calculate_totals();

return $order;
}

/**
* Maybe update shipping rates for multicurrency.
*
* @param array $rate_info The rate response information.
* @param string $wc_currency The default WooCommerce currency.
* @param string $currency The customer currency.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
function fastwc_maybe_update_shipping_rate_for_multicurrency( $rate_info, $wc_currency, $currency, $request ) {
$multicurrency_disabled = fastwc_is_multicurrency_support_disabled();

// Do nothing if multicurrency is disabled.
if (
false === $multicurrency_disabled
&& ! empty( $currency ) // Make sure the customer currency is set.
&& $wc_currency !== $currency // Make sure the customer currency is not the default currency.
) {
/**
* Update shipping rates for multicurrency.
*
* @param array $rate_info The rate response information.
* @param string $currency The customer currency.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
$rate_info = apply_filters(
'fastwc_update_shipping_rate_for_multicurrency',
$rate_info,
$currency,
$request
);
}

return $rate_info;
}
121 changes: 121 additions & 0 deletions includes/multicurrency/class-base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Multicurrency support plugin base class.
*
* @package Fast
*/

namespace FastWC\Multicurrency;

/**
* Base class for supporting third party multicurrency plugins.
*/
class Base {

/**
* Create the class.
*/
public function __construct() {
add_filter( 'fastwc_update_price_for_multicurrency', array( $this, 'update_price' ), 10, 4 );
add_filter( 'fastwc_update_shipping_rate_for_multicurrency', array( $this, 'update_shipping' ), 10, 3 );
add_filter( 'fastwc_update_order_for_multicurrency', array( $this, 'update_order' ), 10, 2 );
}

/**
* Check if the third-party plugin is active.
*
* @return bool
*/
protected function is_active() {
return false;
}

/**
* Filter handler for updating the product price for multicurrency.
*
* @param string $price Value of the price.
* @param WC_Product $product The product object.
* @param WC_Data $order The order to check.
* @param WC_Request $request Request object.
*
* @return string
*/
public function update_price( $price, $product, $order, $request ) {

if ( $this->is_active() ) {
return $this->do_update_price( $price, $product, $order, $request );
}

return $price;
}

/**
* Update the product price for multicurrency.
*
* @param string $price Value of the price.
* @param WC_Product $product The product object.
* @param WC_Data $order The order to check.
* @param WC_Request $request Request object.
*
* @return string
*/
protected function do_update_price( $price, $product, $order, $request ) {
return $price;
}

/**
* Filter handler for updating the shipping rate for multicurrency.
*
* @param array $rate_info The rate response information.
* @param string $currency The customer currency.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
public function update_shipping( $rate_info, $currency, $request ) {

if ( $this->is_active() ) {
return $this->do_update_shipping( $rate_info, $currency, $request );
}

return $rate_info;
}

/**
* Update the shipping rate for multicurrency.
*
* @param array $rate_info The rate response information.
* @param string $currency The customer currency.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
protected function do_update_shipping( $rate_info, $currency, $request ) {
return $rate_info;
}

/**
* Filter handler for updating the order for multicurrency.
*
* @param WC_Data $order The order to check.
* @param WP_REST_Request $request Request object.
*/
public function update_order( $order, $request ) {

if ( $this->is_active() ) {
return $this->do_update_order( $order, $request );
}

return $order;
}

/**
* Update the order for multicurrency.
*
* @param WC_Data $order The order to check.
* @param WP_REST_Request $request Request object.
*/
protected function do_update_order( $order, $request ) {
return $order;
}
}
70 changes: 70 additions & 0 deletions includes/multicurrency/class-currency-switcher-woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Add support for Currency Switcher for WooCommerce plugin.
*
* @see https://wordpress.org/plugins/currency-switcher-woocommerce/
*
* @package Fast
*/

namespace FastWC\Multicurrency;

/**
* Class to support Currency Switcher for WooCommerce.
*/
class Currency_Switcher_Woocommerce extends Base {

/**
* Check if the third-party plugin is active.
*
* @return bool
*/
protected function is_active() {
return class_exists( 'Alg_WC_Currency_Switcher' );
}

/**
* Update the product price for multicurrency.
*
* @param string $price Value of the price.
* @param WC_Product $product The product object.
* @param WC_Data $order The order to check.
* @param WC_Request $request Request object.
*
* @return string
*/
protected function do_update_price( $price, $product, $order, $request ) {

$order_currency = \fastwc_get_order_currency( $order );
$new_price = \alg_get_product_price_by_currency( $price, $order_currency, $product, true );

if ( ! empty( $new_price ) ) {
return $new_price;
}

return $price;
}

/**
* Update the shipping rate for multicurrency.
*
* @param array $rate_info The rate response information.
* @param string $currency The customer currency.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
protected function do_update_shipping( $rate_info, $currency, $request ) {

$rate_info['price'] = \alg_get_product_price_by_currency_global( $rate_info['price'], $currency );
if ( ! empty( $rate_info['taxes'] ) ) {
$rate_taxes = $rate_info['taxes'];

foreach ( $rate_taxes as $rate_tax_id => $rate_tax ) {
$rate_info['taxes'][ $rate_tax_id ] = \alg_get_product_price_by_currency_global( $rate_tax, $currency );
}
}

return $rate_info;
}
}
Loading