1

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Victor Sokoliuk
  • 435
  • 4
  • 17
  • First code is incomplete and not reproducible, remember that your *"question should be updated to include desired behavior, a specific problem or error, and **the shortest code necessary to reproduce the problem"***. Also the method `get_formatted_line_subtotal()` requires a mandatory argument `$item` which is the `WC_Order_Item_Product` Object and so in your code it throw an error… For order "line" items see [*Get Order items and WC_Order_Item_Product in WooCommerce 3*](https://stackoverflow.com/questions/45706007/get-order-items-and-wc-order-item-product-in-woocommerce-3/45706318#45706318) – LoicTheAztec Nov 04 '20 at 12:01

1 Answers1

0

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399