Last active
July 8, 2021 08:51
-
-
Save mmackh/4503fe1caf15f1a1c38713d240836425 to your computer and use it in GitHub Desktop.
Role Based Pricing - Manual Order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function woocommerce_ajax_add_order_item_meta( $item_id, $raw_item, $order ) { | |
$product = $raw_item->get_product(); | |
$rolePrices = $product->get_meta('_role_based_price' ); | |
if (!$rolePrices) return; | |
$userRole = array_shift($order->get_user()->roles); | |
if (!isset($rolePrices[$userRole])) return; | |
$grossPrice = $product->get_price_including_tax(); | |
$netPrice = $product->get_price_excluding_tax(); | |
$taxRate = $grossPrice / $netPrice; | |
if ($taxRate < 1) $taxRate = 1; | |
$regularPrice = wc_format_decimal($rolePrices[$userRole]['regular_price']); | |
$price = $regularPrice / $taxRate; | |
$item = $order->get_item($item_id, false); | |
$item->set_subtotal($price); | |
$item->set_total($price * $item->get_quantity()); | |
$item->save(); | |
} | |
add_action( 'woocommerce_ajax_add_order_item_meta', 'woocommerce_ajax_add_order_item_meta', 99, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment