Calculated Fields

Calculated fields are read-only and can be used to display information that is helpful when bulk editing products. Follow these instructions to add a calculated field to the Bulk Editor:

1. Download the free Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
2. Create a Snippet with the following code (Note: If you are more comfortable with editing your functions.php you can do that instead of Code Snippets.):

function pwbe_product_columns_atum_inbound_stock( $columns ) {
    $columns[] = array(
        'name' => 'Calculated Field',
        'type' => 'text',
        'table' => 'calculated',
        'field' => 'example_calculated_field',
        'visibility' => 'both',
        'readonly' => true,
        'sortable' => 'false'
    );

    return $columns;
}
function pwbe_results_product_atum_inbound_stock( $pwbe_product, $column ) {
    if ( $column['field'] == 'example_calculated_field' ) {

        // Display regular price with a 10% increase.
        $result = $pwbe_product->_regular_price * 1.10;

        $pwbe_product->example_calculated_field = $result;
    }

    return $pwbe_product;
}
add_filter( 'pwbe_product_columns', 'pwbe_product_columns_atum_inbound_stock' );
add_filter( 'pwbe_results_product', 'pwbe_results_product_atum_inbound_stock', 10, 2 );

Example: ATUM Inbound Stock
If you use the ATUM Inventory Stock Management plugin, you can add the Inbound Stock calculated field using the following snippet:

function pwbe_product_columns_atum_inbound_stock( $columns ) {
    $columns[] = array(
        'name' => 'Inbound Stock',
        'type' => 'text',
        'table' => 'calculated',
        'field' => 'atum_inbound_stock',
        'visibility' => 'both',
        'readonly' => true,
        'sortable' => 'false'
    );

    return $columns;
}
function pwbe_results_product_atum_inbound_stock( $pwbe_product, $column ) {
    if ( $column['field'] == 'atum_inbound_stock' ) {
        $atum_product = \Atum\Inc\Helpers::get_atum_product( $pwbe_product->post_id );
        $inbound_stock = $atum_product->get_inbound_stock();

        $pwbe_product->atum_inbound_stock = $inbound_stock;
    }

    return $pwbe_product;
}
add_filter( 'pwbe_product_columns', 'pwbe_product_columns_atum_inbound_stock' );
add_filter( 'pwbe_results_product', 'pwbe_results_product_atum_inbound_stock', 10, 2 );