parikhdeeshit
parikhdeeshit

Reputation: 11

How to calculate line total in woocommerce

How can I calculate the line total for creating order in woo commerce if i am using coupon code of 20 rs off and and having tax of 6%.

$order = new WC_Order();
$order = wc_create_order($order_data);

$items = WC()->cart->get_cart();

foreach($items as $item => $values) {
    $product_id = $values['product_id'];
    $product = wc_get_product($product_id);
    $quantity = (int)$values['quantity'];

    $sale_price = $product->get_price();
    $final_price =  ?????
    $price_params = array( 'totals' => array( 'subtotal' => $sale_price, 'total' => $final_price ) );
    $order->add_product($product, $quantity,$price_params);
}

Here how can I get $final_price ?

Upvotes: 1

Views: 4753

Answers (2)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

Try This

public function get_line_total( $item, $inc_tax = false, $round = true ) {
    $total = 0;

    if ( is_callable( array( $item, 'get_total' ) ) ) {
      // Check if we need to add line tax to the line total.
      $total = $inc_tax ? $item->get_total() + $item->get_total_tax() : $item->get_total();

      // Check if we need to round.
      $total = $round ? round( $total, wc_get_price_decimals() ) : $total;
    }

    return apply_filters( 'woocommerce_order_amount_line_total', $total, $this, $item, $inc_tax, $round );
  }

Upvotes: 2

Pranav Bhatt
Pranav Bhatt

Reputation: 745

I think you could do something like this

$order = new WC_Order();
$order = wc_create_order($order_data);

$items = WC()->cart->get_cart();

foreach($items as $item => $values) {
    $product_id = $values['product_id'];
    $product = wc_get_product($product_id);
    $quantity = (int)$values['quantity'];

    $sale_price = $product->get_price();
    $final_price =  ?????
    $price_params = array( 'totals' => array( 'subtotal' => $sale_price, 'total' => $final_price ) );
    $order->add_product($product, $quantity,$price_params);
}
$order_total=WC()->cart->calculate_totals();

Upvotes: 0

Related Questions