Add fees when creating line items from cart

This commit is contained in:
emilicastells 2021-08-13 16:27:52 +02:00
parent efcbf5dd7d
commit 2273213cae
No known key found for this signature in database
GPG key ID: 1520C07081754570
3 changed files with 33 additions and 4 deletions

View file

@ -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(),

View file

@ -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 );
}
/**

View file

@ -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 );
}
}
);
}
/**