Hi, i am testing with this code in my functions.php. It seems to work well. But I want the author to check it and confirm if it is correct.
function update_order_prices_on_admin_ajax( $item_id, $item, $order ){
foreach ( $order->get_items() as $order_item_id => $order_item_data ) {
if ( $order_item_id == $item_id ) {
$newRegularPrice = false;
$rolePrices = $order_item_data->get_product()->get_meta('_role_based_price' );
$userRoles = $order->get_user()->roles;
if(in_array('administrator', $userRoles)){
if (isset($rolePrices['administrator']['regular_price'])){
$newRegularPrice = $rolePrices['administrator']['regular_price'];
}
}elseif(in_array('editor', $userRoles)){
if (isset($rolePrices['editor']['regular_price'])){
$newRegularPrice = $rolePrices['editor']['regular_price'];
}
}
if($newRegularPrice){
$order_item_data->set_subtotal($newRegularPrice);
$order_item_data->set_total($newRegularPrice*$order_item_data->get_quantity());
$order_item_data->calculate_taxes();
$order_item_data->apply_changes();
$order_item_data->save();
}
}
}
// Runs this after making a change to $order_item_data
$order->calculate_totals();
$order->apply_changes();
$order->save();
}
add_action( 'woocommerce_ajax_add_order_item_meta', 'update_order_prices_on_admin_ajax', 99, 3 );
Thanks