mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Merge pull request #221 from woocommerce/PCP-195-plugin-conflict-blocking-line-it
Plugin conflict blocking line item details
This commit is contained in:
commit
44aecb698e
5 changed files with 103 additions and 7 deletions
|
@ -45,9 +45,18 @@ class AmountFactory {
|
|||
* @return Amount
|
||||
*/
|
||||
public function from_wc_cart( \WC_Cart $cart ): Amount {
|
||||
$currency = get_woocommerce_currency();
|
||||
$total = new Money( (float) $cart->get_total( 'numeric' ), $currency );
|
||||
$item_total = $cart->get_cart_contents_total() + $cart->get_discount_total();
|
||||
$currency = get_woocommerce_currency();
|
||||
$total = new Money( (float) $cart->get_total( 'numeric' ), $currency );
|
||||
|
||||
$total_fees_amount = 0;
|
||||
$fees = WC()->session->get( 'ppcp_fees' );
|
||||
if ( $fees ) {
|
||||
foreach ( WC()->session->get( 'ppcp_fees' ) as $fee ) {
|
||||
$total_fees_amount += (float) $fee->amount;
|
||||
}
|
||||
}
|
||||
|
||||
$item_total = $cart->get_cart_contents_total() + $cart->get_discount_total() + $total_fees_amount;
|
||||
$item_total = new Money( (float) $item_total, $currency );
|
||||
$shipping = new Money(
|
||||
(float) $cart->get_shipping_total() + $cart->get_shipping_tax(),
|
||||
|
|
|
@ -56,7 +56,25 @@ class ItemFactory {
|
|||
},
|
||||
$cart->get_cart_contents()
|
||||
);
|
||||
return $items;
|
||||
|
||||
$fees = array();
|
||||
$fees_from_session = WC()->session->get( 'ppcp_fees' );
|
||||
if ( $fees_from_session ) {
|
||||
$fees = array_map(
|
||||
static function ( \stdClass $fee ) use ( $currency ): Item {
|
||||
return new Item(
|
||||
$fee->name,
|
||||
new Money( (float) $fee->amount, $currency ),
|
||||
1,
|
||||
'',
|
||||
new Money( (float) $fee->tax, $currency )
|
||||
);
|
||||
},
|
||||
$fees_from_session
|
||||
);
|
||||
}
|
||||
|
||||
return array_merge( $items, $fees );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,12 +84,21 @@ class ItemFactory {
|
|||
* @return Item[]
|
||||
*/
|
||||
public function from_wc_order( \WC_Order $order ): array {
|
||||
return array_map(
|
||||
$items = array_map(
|
||||
function ( \WC_Order_Item_Product $item ) use ( $order ): Item {
|
||||
return $this->from_wc_order_line_item( $item, $order );
|
||||
},
|
||||
$order->get_items( 'line_item' )
|
||||
);
|
||||
|
||||
$fees = array_map(
|
||||
function ( \WC_Order_Item_Fee $item ) use ( $order ): Item {
|
||||
return $this->from_wc_order_fee( $item, $order );
|
||||
},
|
||||
$order->get_fees()
|
||||
);
|
||||
|
||||
return array_merge( $items, $fees );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,6 +136,24 @@ class ItemFactory {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Item based off a WooCommerce Fee Item.
|
||||
*
|
||||
* @param \WC_Order_Item_Fee $item The WooCommerce order item.
|
||||
* @param \WC_Order $order The WooCommerce order.
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
private function from_wc_order_fee( \WC_Order_Item_Fee $item, \WC_Order $order ): Item {
|
||||
return new Item(
|
||||
$item->get_name(),
|
||||
new Money( (float) $item->get_amount(), $order->get_currency() ),
|
||||
$item->get_quantity(),
|
||||
'',
|
||||
new Money( (float) $item->get_total_tax(), $order->get_currency() )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Item based off a PayPal response.
|
||||
*
|
||||
|
|
|
@ -10,7 +10,6 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\ApiClient;
|
||||
|
||||
use Dhii\Container\ServiceProvider;
|
||||
use Dhii\Modular\Module\Exception\ModuleExceptionInterface;
|
||||
use Dhii\Modular\Module\ModuleInterface;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
@ -38,6 +37,13 @@ class ApiModule implements ModuleInterface {
|
|||
* @param ContainerInterface $container The container.
|
||||
*/
|
||||
public function run( ContainerInterface $container ): void {
|
||||
add_action(
|
||||
'woocommerce_after_calculate_totals',
|
||||
function ( \WC_Cart $cart ) {
|
||||
$fees = $cart->fees_api()->get_fees();
|
||||
WC()->session->set( 'ppcp_fees', $fees );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,13 +3,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use function Brain\Monkey\Functions\when;
|
||||
|
||||
class AmountFactoryTest extends TestCase
|
||||
{
|
||||
|
@ -45,6 +45,13 @@ class AmountFactoryTest extends TestCase
|
|||
->andReturn(7);
|
||||
|
||||
expect('get_woocommerce_currency')->andReturn($expectedCurrency);
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
when('WC')->justReturn($woocommerce);
|
||||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
$this->assertEquals($expectedCurrency, $result->currency_code());
|
||||
$this->assertEquals((float) 1, $result->value());
|
||||
|
@ -90,6 +97,12 @@ class AmountFactoryTest extends TestCase
|
|||
->andReturn(0);
|
||||
|
||||
expect('get_woocommerce_currency')->andReturn($expectedCurrency);
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
when('WC')->justReturn($woocommerce);
|
||||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
$this->assertNull($result->breakdown()->discount());
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use function Brain\Monkey\Functions\when;
|
||||
use Mockery;
|
||||
|
||||
class ItemFactoryTest extends TestCase
|
||||
|
@ -51,6 +52,13 @@ class ItemFactoryTest extends TestCase
|
|||
expect('wp_strip_all_tags')
|
||||
->with('description')
|
||||
->andReturn('description');
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
when('WC')->justReturn($woocommerce);
|
||||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
|
@ -108,6 +116,12 @@ class ItemFactoryTest extends TestCase
|
|||
->with('description')
|
||||
->andReturn('description');
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
when('WC')->justReturn($woocommerce);
|
||||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
|
||||
$item = current($result);
|
||||
|
@ -158,6 +172,9 @@ class ItemFactoryTest extends TestCase
|
|||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
->andReturn(1);
|
||||
$order
|
||||
->expects('get_fees')
|
||||
->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_order($order);
|
||||
$this->assertCount(1, $result);
|
||||
|
@ -218,6 +235,9 @@ class ItemFactoryTest extends TestCase
|
|||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
->andReturn(1);
|
||||
$order
|
||||
->expects('get_fees')
|
||||
->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_order($order);
|
||||
$item = current($result);
|
||||
|
@ -273,6 +293,9 @@ class ItemFactoryTest extends TestCase
|
|||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
->andReturn(1);
|
||||
$order
|
||||
->expects('get_fees')
|
||||
->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_order($order);
|
||||
$item = current($result);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue