Php 如何计算商业中的行总数

Php 如何计算商业中的行总数,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,如果我使用的优惠券代码为20卢比,税率为6%,如何计算在woo commerce中创建订单的行总数 $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

如果我使用的优惠券代码为20卢比,税率为6%,如何计算在woo commerce中创建订单的行总数

$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);
}

这里我怎么才能拿到$final\u价格?

我想你可以这样做

$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();
试试这个

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 );
  }

使用WC_购物车::计算_总计();是的,我已经添加了这个,但当我可以登记管理员没有折扣价格被添加。和折扣券,当它适用于每个产品线项目。。。因此,我们必须添加带有参数的产品..您可以在woocommerce设置的优惠券部分使用固定购物车折扣设置折扣类型。我已经添加了购物车折扣,但当我创建订单时,我对整个购物车有20卢比的折扣和6%的税。产品价格是50卢比。那么我怎样才能得到这个产品的最终价格呢。??