如何计算wocommerce中的行总数

时间:2017-04-19 12:51:25

标签: php wordpress woocommerce

如果我使用的优惠券代码为20 rs off且税率为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_price ??

2 个答案:

答案 0 :(得分:0)

我认为你可以做这样的事情

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

答案 1 :(得分:0)

试试这个

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