Ecommerce Shopify WordPress Discussion

Woocommerce how to apply discount on order line item based on order item meta

I'd like to apply a discount on an order item from the admin panel by using the meta field on the product. There is the option to modify the input price on the right, but you can't do it with a percentage. So my idea is to apply a discount function if exists a meta field. This is my function, but it is not working: function discount_meta($and_taxes, $order){ // Go through Order items foreach ($order->get_items() as $item_id => $item) { //Update Line Subtotal if exist discount meta if (wc_get_order_item_meta($item_id, 'discount')) { $discount = wc_get_order_item_meta($item_id, 'discount'); $discountval = $price * $discount / 100; $saleprice = $price - $discountval; wc_update_order_item_meta($item_id, '_line_total', $saleprice); } } } add_action('woocommerce_order_before_calculate_totals', 'discount_meta', 10, 3); On the admin panel I'd like to do this:
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.