Thijmen van Doorn
Thijmen van Doorn

Reputation: 131

Woocommerce get price after calculation

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,

Upvotes: 2

Views: 402

Answers (1)

Vincenzo Di Gaetano
Vincenzo Di Gaetano

Reputation: 4100

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:

Upvotes: 2

Related Questions