Skip to content

Commit

Permalink
Tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
asumaran committed Dec 18, 2024
1 parent b0c8669 commit 4ad046f
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,24 @@ public function build_display_items( $itemized_display_items = false ) {
}
}

$discounts = wc_format_decimal( $discounts, WC()->cart->dp );
$tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp );
$shipping = wc_format_decimal( WC()->cart->shipping_total, WC()->cart->dp );
$items_total = wc_format_decimal( WC()->cart->cart_contents_total, WC()->cart->dp ) + $discounts;

// This is a temporary fix that should be removed later when we fix the amounts in the item list.
// The reason for this fix is that amount ECE uses on the product page is calculated using the subscription price + the signup fee.
// However, in this case, it is calculated based solely on the cart total.
// For subscriptions with a free trial and a signup fee, the cart does not account for the total item value, only the signup fee.
// To ensure the amounts in both contexts are synchronized, the total item value in the cart will be manually calculated and added to the $order_total,
// but only for the specific case of subscriptions with a free trial and a signup fee.
$items_total_calculated = 0;
$items_total_adjust = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::get_sign_up_fee( $cart_item['product_id'] ) > 0 && WC_Subscriptions_Product::get_sign_up_fee( $cart_item['product_id'] ) > 0 ) {
$items_total_calculated += wc_get_product( $cart_item['product_id'] )->get_price();
$items_total_adjust += wc_get_product( $cart_item['product_id'] )->get_price();
}
}
$added_to_cart_amount = $items_total_calculated;

$order_total = version_compare( WC_VERSION, '3.2', '<' ) ? wc_format_decimal( $items_total + $added_to_cart_amount + $tax + $shipping - $discounts, WC()->cart->dp ) : WC()->cart->get_total( '' ) + $added_to_cart_amount;
$discounts = wc_format_decimal( $discounts, WC()->cart->dp );
$tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp );
$shipping = wc_format_decimal( WC()->cart->shipping_total, WC()->cart->dp );
$items_total = wc_format_decimal( WC()->cart->cart_contents_total, WC()->cart->dp ) + $discounts;
$order_total = version_compare( WC_VERSION, '3.2', '<' ) ? wc_format_decimal( $items_total + $items_total_adjust + $tax + $shipping - $discounts, WC()->cart->dp ) : WC()->cart->get_total( '' ) + $items_total_adjust;

if ( ! $this->cart_prices_include_tax() ) {
$items[] = [
Expand Down

0 comments on commit 4ad046f

Please sign in to comment.