2017-04-19 50 views
0

如何使用優惠券代碼20 rs off和6%的稅率來計算在uoo 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?

+0

使用WC_Cart :: calculate_totals(); –

回答

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(); 
+0

是的,我已經添加了這個,但是當我可以檢查管理員沒有折扣價格添加。當應用折扣優惠券時,它適用於每個產品系列商品...因此我們必須添加具有參數的商品。 – parikhdeeshit

+0

您可以在woocommerce設置的優惠券部分中使用固定購物車折扣設置折扣類型。 –

+0

我已添加購物車折扣,但是當我創建訂單時,我已爲整個購物車和6%的稅率打折20盧比。產品價格爲50盧比。那麼我怎麼能得到這個產品的最終價格? – parikhdeeshit

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