Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility for Multi-Currency and FedEx plugin. #3131

Merged
merged 16 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
fc89806
Add compatibility for Multi-Currency and FedEx plugin.
jessepearson Oct 15, 2021
fc3936c
Add compatibility for Multi-Currency and UPS plugin.
jessepearson Oct 15, 2021
9556475
Merge branch 'develop' into add/3132-multi-currency-compatibility-ups
jessepearson Oct 18, 2021
e26323f
Merge branch 'develop' into add/3128-multi-currency-compatibility-fedex
jessepearson Oct 18, 2021
fa447fa
Adding tests for Multi-Currency and UPS compatibility.
jessepearson Oct 18, 2021
8b79894
Merge branch 'develop' into add/3132-multi-currency-compatibility-ups
jessepearson Oct 18, 2021
5704db9
Adding tests for Multi-Currency and UPS compatibility.
jessepearson Oct 18, 2021
0ab40ad
Adding tests for Multi-Currency and UPS compatibility.
jessepearson Oct 18, 2021
e99454c
Merge branch 'develop' into add/3128-multi-currency-compatibility-fedex
jessepearson Oct 18, 2021
fd242b7
Adding tests for Multi-Currency and FedEx compatibility.
jessepearson Oct 18, 2021
b2ab285
Merge branch 'add/3132-multi-currency-compatibility-ups' into add/312…
jessepearson Oct 18, 2021
b234a14
Update changelog and readme.
jessepearson Oct 19, 2021
562750d
Merge branch 'develop' into add/3128-multi-currency-compatibility-fedex
jessepearson Oct 20, 2021
ca4c8d2
Fix extra line in changelog, added missing return type declarations.
jessepearson Oct 20, 2021
27629c6
Merge branch 'develop' into add/3128-multi-currency-compatibility-fedex
jessepearson Oct 21, 2021
9bd52f5
Merge branch 'develop' into add/3128-multi-currency-compatibility-fedex
jessepearson Oct 21, 2021
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Fix - Multi-Currency settings rounding option and preview.
* Fix - Payment failure on checkout block with UPE when phone number field is hidden
* Add - Add compatibility between Multi-Currency and WooCommerce UPS shipping extension.
* Add - Add compatibility between Multi-Currency and WooCommerce FedEx shipping extension.
* Fix - Fix decimal error with shipping calculations with Multi-Currency.

= 3.1.0 - 2021-10-06 =
Expand Down
2 changes: 2 additions & 0 deletions includes/multi-currency/Compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use WC_Order;
use WC_Order_Refund;
use WCPay\MultiCurrency\Compatibility\WooCommerceFedEx;
use WCPay\MultiCurrency\Compatibility\WooCommerceUPS;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function __construct( MultiCurrency $multi_currency, Utils $utils ) {
add_filter( 'woocommerce_admin_sales_record_milestone_enabled', [ $this, 'attach_order_modifier' ] );
}

$compatibility_classes[] = new WooCommerceFedEx( $multi_currency, $utils );
$compatibility_classes[] = new WooCommerceUPS( $multi_currency, $utils );
}

Expand Down
85 changes: 85 additions & 0 deletions includes/multi-currency/Compatibility/WooCommerceFedEx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Class WooCommerceFedEx
*
* @package WCPay\MultiCurrency\Compatibility
*/

namespace WCPay\MultiCurrency\Compatibility;

use WC_Product;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;

/**
* Class that controls Multi Currency Compatibility with WooCommerce FedEx Plugin.
*/
class WooCommerceFedEx {

const FILTER_PREFIX = 'wcpay_multi_currency_';

/**
* MultiCurrency class.
*
* @var MultiCurrency
*/
private $multi_currency;

/**
* Utils class.
*
* @var Utils
*/
private $utils;

/**
* Constructor.
*
* @param MultiCurrency $multi_currency MultiCurrency class.
* @param Utils $utils Utils class.
*/
public function __construct( MultiCurrency $multi_currency, Utils $utils ) {
$this->multi_currency = $multi_currency;
$this->utils = $utils;
$this->initialize_hooks();
}

/**
* Adds compatibility filters if the plugin exists and loaded
*
* @return void
*/
protected function initialize_hooks() {
if ( class_exists( 'WC_Shipping_Fedex_Init' ) ) {
add_filter( self::FILTER_PREFIX . 'should_return_store_currency', [ $this, 'should_return_store_currency' ] );
}
}

/**
* Determine whether to return the store currency or not.
*
* @param bool $return Whether to return the store currency or not.
*
* @return bool
*/
public function should_return_store_currency( bool $return ): bool {
// If it's already true, return it.
if ( $return ) {
return $return;
}

$calls = [
'WC_Shipping_Fedex->set_settings',
'WC_Shipping_Fedex->per_item_shipping',
'WC_Shipping_Fedex->box_shipping',
'WC_Shipping_Fedex->get_fedex_api_request',
'WC_Shipping_Fedex->get_fedex_requests',
'WC_Shipping_Fedex->process_result',
];
if ( $this->utils->is_call_in_backtrace( $calls ) ) {
return true;
}

return $return;
}
}
2 changes: 1 addition & 1 deletion includes/multi-currency/Compatibility/WooCommerceUPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function initialize_hooks() {
*
* @return bool
*/
public function should_return_store_currency( bool $return ) {
public function should_return_store_currency( bool $return ): bool {
// If it's already true, return it.
if ( $return ) {
return $return;
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Please note that our support for the checkout block is still experimental and th
* Fix - Multi-Currency settings rounding option and preview.
* Fix - Payment failure on checkout block with UPE when phone number field is hidden
* Add - Add compatibility between Multi-Currency and WooCommerce UPS shipping extension.
* Add - Add compatibility between Multi-Currency and WooCommerce FedEx shipping extension.
* Fix - Fix decimal error with shipping calculations with Multi-Currency.

= 3.1.0 - 2021-10-06 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Class WCPay_Multi_Currency_WooCommerceFedEx_Tests
*
* @package WooCommerce\Payments\Tests
*/

use WCPay\MultiCurrency\Compatibility\WooCommerceFedEx;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;

/**
* WCPay\MultiCurrency\Compatibility\WooCommerceFedEx unit tests.
*/
class WCPay_Multi_Currency_WooCommerceFedEx_Tests extends WP_UnitTestCase {

/**
* Mock WCPay\MultiCurrency\MultiCurrency.
*
* @var WCPay\MultiCurrency\MultiCurrency|PHPUnit_Framework_MockObject_MockObject
*/
private $mock_multi_currency;

/**
* Mock WCPay\MultiCurrency\Utils.
*
* @var WCPay\MultiCurrency\Utils|PHPUnit_Framework_MockObject_MockObject
*/
private $mock_utils;

/**
* WCPay\MultiCurrency\Compatibility\WooCommerceFedEx instance.
*
* @var WCPay\MultiCurrency\Compatibility\WooCommerceFedEx
*/
private $woocommerce_fedex;

/**
* Pre-test setup
*/
public function setUp() {
parent::setUp();

$this->mock_multi_currency = $this->createMock( MultiCurrency::class );
$this->mock_utils = $this->createMock( Utils::class );
$this->woocommerce_fedex = new WooCommerceFedEx( $this->mock_multi_currency, $this->mock_utils );
}

// If true is passed, it should automatically return true.
public function test_should_return_store_currency_returns_true_if_true_passed() {
$this->mock_utils->expects( $this->exactly( 0 ) )->method( 'is_call_in_backtrace' );
$this->assertTrue( $this->woocommerce_fedex->should_return_store_currency( true ) );
}

// If the calls are found, it should return true.
public function test_should_return_store_currency_returns_true_if_calls_found() {
$calls = [
'WC_Shipping_Fedex->set_settings',
'WC_Shipping_Fedex->per_item_shipping',
'WC_Shipping_Fedex->box_shipping',
'WC_Shipping_Fedex->get_fedex_api_request',
'WC_Shipping_Fedex->get_fedex_requests',
'WC_Shipping_Fedex->process_result',
];
$this->mock_utils
->expects( $this->once() )
->method( 'is_call_in_backtrace' )
->with( $calls )
->willReturn( true );
$this->assertTrue( $this->woocommerce_fedex->should_return_store_currency( false ) );
}

// If the calls are found, it should return true.
public function test_should_return_store_currency_returns_false_if_no_calls_found() {
$calls = [
'WC_Shipping_Fedex->set_settings',
'WC_Shipping_Fedex->per_item_shipping',
'WC_Shipping_Fedex->box_shipping',
'WC_Shipping_Fedex->get_fedex_api_request',
'WC_Shipping_Fedex->get_fedex_requests',
'WC_Shipping_Fedex->process_result',
];
$this->mock_utils
->expects( $this->once() )
->method( 'is_call_in_backtrace' )
->with( $calls )
->willReturn( false );
$this->assertFalse( $this->woocommerce_fedex->should_return_store_currency( false ) );
}
}