【问题标题】:How to calculate line total in woocommerce如何计算woocommerce中的总行数
【发布时间】:2017-04-19 12:51:24
【问题描述】:

如果我使用 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 ?

【问题讨论】:

  • 使用 WC_Cart::calculate_totals();

标签: php wordpress woocommerce


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    我认为你可以这样做

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

    【讨论】:

    • 是的,我已经添加了这个,但是当我可以在管理员中签入时,没有添加折扣价。并且折扣券在应用时适用于每个产品行项目......所以我们必须添加带有参数的产品......
    • 你可以在woocommerce设置的优惠券部分设置具有固定购物车折扣的折扣类型。
    • 我已经添加了购物车折扣,但是当我创建订单时,我有整个购物车的 20 卢比折扣和 6% 的税费。产品价格为 50 卢比。那么我怎样才能得到这个产品的最终价格。??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多