2

I need the product price after calculation with other plugins. As an example, the regular price is 1.50 but een custom price plugin adds some fees so the product price is 2.50.

I want the product price on a invoice with code ($product_price) below, but i get 1.50. I want, 2.50. How do i get this?

$productnummer_id = $order->get_item_meta($item_id, '_product_id', true);

    $_product = wc_get_product( $productnummer_id );
    $product_price  = $_product->get_price();
    $old_product_price_META = $rekensom1;

    $line_items[] = array(
        
            'description' => $item_data['name'],
            'units' => $order->get_item_meta($item_id, '_qty', true),
            'amount_per_unit'=>array(
                'value' => $product_price,
                'currency'=> 'EUR'
            ),
            'vat' => $tax,

1 Answers1

1

If you use $_product->get_price(); you get the price from the WC_Product object and not from WC_Order_Item_Product.

To get the price of the order item, in your case, you can do this:

$item = new WC_Order_Item_Product( $item_id );

$line_total             = $item->get_total(); // Line total
$quantity               = $item->get_quantity();
$product_price          = $line_total / $quantity; // Total unit price

$line_items[] = array(
    
        'description' => $item_data['name'],
        'units' => $quantity,
        'amount_per_unit'=>array(
            'value' => $product_price,
            'currency'=> 'EUR'
        ),
        'vat' => $tax,

Note that get_item_meta() is a deprecated function.

For more information you can view these complete answers:

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • Thanks! At the moment I am indeed following the current method. Dividing the quantity by the line_total, but I am getting rounding problems causing the amount to differ by 1 or 2 cents from the order total. This probably has to do with the rounding of numbers and that is why I am looking for the product price per piece. – Thijmen van Doorn Jan 20 '21 at 09:41
  • For price rounding take a look here: https://stackoverflow.com/questions/61836970/how-can-i-round-up-the-price-in-woocommerce – Vincenzo Di Gaetano Jan 20 '21 at 12:42