mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-10 01:15:45 +08:00
Add fees when creating line items from cart
This commit is contained in:
parent
efcbf5dd7d
commit
2273213cae
3 changed files with 33 additions and 4 deletions
|
@ -47,7 +47,13 @@ class AmountFactory {
|
|||
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();
|
||||
|
||||
$total_fees_amount = 0;
|
||||
foreach ( WC()->session->get( '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,21 @@ class ItemFactory {
|
|||
},
|
||||
$cart->get_cart_contents()
|
||||
);
|
||||
return $items;
|
||||
|
||||
$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 )
|
||||
);
|
||||
},
|
||||
WC()->session->get( 'fees' )
|
||||
);
|
||||
|
||||
return array_merge( $items, $fees );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -38,6 +38,15 @@ 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();
|
||||
if ( $fees ) {
|
||||
WC()->session->set( 'fees', $fees );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue