Skip to content
Advertisement

Get non formatted value from $order->get_formatted_line_subtotal() in WooCommerce

I need to make certain changes to the file order-details-item.php Product prices are formed in custom meta fields. So in my case the value is: $qty = $item->get_quantity(); is incorrect. It is always the same.

To solve the problem, I can use the simplest arephmetic operation. Divide the total order price by the product price. For example, if a customer ordered 10 kilograms of apples at a cost of 14.5 per kilogram the total cost will be 145. This means that in order to correctly display the quantity I need 145/10.

    $price_weight_array = $product_attr['_mia_cup_price_weight'];
    $price_unit_array = $product_attr['_mia_cup_price_unit'];
    $sale_price_array = $product_attr['_mia_cup_sale_price_unit'];  
    
    $price_weight = $price_weight_array[0];
    $price_unit   = $price_unit_array[0];
    $sale_price = $sale_price_array[0];
    $prod_item_total = $order->get_formatted_line_subtotal();

    custom_quantity = $prod_item_total / $price_weight

And here is the problem $order->get_formatted_line_subtotal(); returns me a number with currency symbol. And I cannot use it in arithmetic operation. How can I remove this symbol?

Advertisement

Answer

Your code is incomplete and you are not using the right way… Also get_formatted_line_subtotal() method requires a mandatory argument in order to work and as you know displays the formatted order item subtotal (with currency symbol)

Based on How to get WooCommerce order details and Get Order items and WC_Order_Item_Product in WooCommerce 3, you need first to get order items and you will use your code in a foreach loop.

To get the non formatted and non rounded order item subtotal you will use instead get_line_subtotal() method as follows:

$order = wc_get_order($order_id); // (optional - if needed) get the WC_Order object

// Loop through order items
foreach( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product(); // get the WC_Product Object

    // Your other code (missing from your question) Here …

    $price_weight    = reset($product_attr['_mia_cup_price_weight']);
    $price_unit      = reset($product_attr['_mia_cup_price_unit']);
    $sale_price_unit = reset($product_attr['_mia_cup_sale_price_unit']); 

    // Get the non formatted order item subtotal (and not rounded)
    $item_subtotal   = $order->get_line_subtotal( $item, $order->get_prices_include_tax(), false );

    $custom_quantity = $item_subtotal / $price_weight;
}

It should better work

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement