Improve item total/price retrieval

Less calculations, also fee was not included in item price for subs with fee
This commit is contained in:
Alex P 2022-06-15 16:30:38 +03:00
parent 24586d0910
commit 02c84e43e0
4 changed files with 53 additions and 117 deletions

View file

@ -69,16 +69,8 @@ class AmountFactory {
public function from_wc_cart( \WC_Cart $cart ): Amount {
$total = new Money( (float) $cart->get_total( 'numeric' ), $this->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, $this->currency );
$item_total = (float) $cart->get_subtotal() + (float) $cart->get_fee_total();
$item_total = new Money( $item_total, $this->currency );
$shipping = new Money(
(float) $cart->get_shipping_total(),
$this->currency
@ -138,13 +130,6 @@ class AmountFactory {
);
}
$items = array_filter(
$items,
function ( Item $item ): bool {
return $item->unit_amount()->value() > 0;
}
);
$total_value = (float) $order->get_total();
if ( (
CreditCardGateway::ID === $order->get_payment_method()
@ -157,13 +142,7 @@ class AmountFactory {
$total = new Money( $total_value, $currency );
$item_total = new Money(
(float) array_reduce(
$items,
static function ( float $total, Item $item ): float {
return $total + $item->quantity() * $item->unit_amount()->value();
},
0
),
(float) $order->get_subtotal(),
$currency
);
$shipping = new Money(

View file

@ -53,12 +53,10 @@ class ItemFactory {
*/
$quantity = (int) $item['quantity'];
$price = (float) wc_get_price_including_tax( $product );
$price_without_tax = (float) wc_get_price_excluding_tax( $product );
$price_without_tax_rounded = round( $price_without_tax, 2 );
$price = (float) $item['line_subtotal'] / (float) $item['quantity'];
return new Item(
mb_substr( $product->get_name(), 0, 127 ),
new Money( $price_without_tax_rounded, $this->currency ),
new Money( $price, $this->currency ),
$quantity,
substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '',
null,
@ -125,7 +123,6 @@ class ItemFactory {
$product = $item->get_product();
$currency = $order->get_currency();
$quantity = (int) $item->get_quantity();
$price = (float) $order->get_item_subtotal( $item, true );
$price_without_tax = (float) $order->get_item_subtotal( $item, false );
$price_without_tax_rounded = round( $price_without_tax, 2 );