• Hi, I have a little problem with the plugin.

    I need to create an order manually for a user. I go to the Backend – Woocommerce – Orders – Manual Create Order. So I create an order, but the prices are always based on the normal price without role.

    Is there a way to use the roles in the backend to calculate the prices.

    Thank you very much

Viewing 1 replies (of 1 total)
  • Thread Starter juanjobt

    (@juanjobt)

    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

Viewing 1 replies (of 1 total)
  • The topic ‘Backend – Orders – Manual Create Order – Roles not supported’ is closed to new replies.