Halloween Sale 15% off Ends in See the plans
PHP

Wholesale Price Field per Product

Adds a "Wholesale price" box to simple products and variations, and charges that price to customers with your wholesale role instead of the retail price.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_wholesale_role = 'wholesale_customer'; // Role that pays wholesale prices.

$wpide_is_wholesale = function () use ( $wpide_wholesale_role ) {
    return is_user_logged_in() && in_array( $wpide_wholesale_role, (array) wp_get_current_user()->roles, true );
};

add_action( 'woocommerce_product_options_pricing', function () {
    global $product_object;

    woocommerce_wp_text_input( array(
        'id'        => '_wpide_wholesale_price',
        'label'     => 'Wholesale price (' . get_woocommerce_currency_symbol() . ')',
        'data_type' => 'price',
        'value'     => $product_object instanceof WC_Product ? $product_object->get_meta( '_wpide_wholesale_price' ) : '',
    ) );
} );

add_action( 'woocommerce_admin_process_product_object', function ( $product ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the product save nonce.
    if ( isset( $_POST['_wpide_wholesale_price'] ) ) {
        $product->update_meta_data( '_wpide_wholesale_price', wc_format_decimal( wp_unslash( $_POST['_wpide_wholesale_price'] ) ) );
    }
} );

add_action( 'woocommerce_variation_options_pricing', function ( $loop, $data, $variation ) {
    woocommerce_wp_text_input( array(
        'id'            => '_wpide_wholesale_price[' . $loop . ']',
        'label'         => 'Wholesale price (' . get_woocommerce_currency_symbol() . ')',
        'data_type'     => 'price',
        'wrapper_class' => 'form-row form-row-full',
        'value'         => get_post_meta( $variation->ID, '_wpide_wholesale_price', true ),
    ) );
}, 10, 3 );

add_action( 'woocommerce_save_product_variation', function ( $variation_id, $i ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the variations save nonce.
    if ( isset( $_POST['_wpide_wholesale_price'][ $i ] ) && current_user_can( 'edit_product', $variation_id ) ) {
        update_post_meta( $variation_id, '_wpide_wholesale_price', wc_format_decimal( wp_unslash( $_POST['_wpide_wholesale_price'][ $i ] ) ) );
    }
}, 10, 2 );

$wpide_wholesale_filter = function ( $price, $product ) use ( $wpide_is_wholesale ) {
    if ( ! $wpide_is_wholesale() ) {
        return $price;
    }

    $wholesale = $product->get_meta( '_wpide_wholesale_price', true, 'edit' );

    return '' !== $wholesale ? $wholesale : $price;
};

foreach ( array( 'woocommerce_product_get_price', 'woocommerce_product_variation_get_price', 'woocommerce_variation_prices_price' ) as $wpide_hook ) {
    add_filter( $wpide_hook, $wpide_wholesale_filter, 99, 2 );
}

add_filter( 'woocommerce_product_is_on_sale', function ( $on_sale, $product ) use ( $wpide_is_wholesale ) {
    return $wpide_is_wholesale() && '' !== $product->get_meta( '_wpide_wholesale_price', true, 'edit' ) ? false : $on_sale;
}, 10, 2 );

add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash ) use ( $wpide_is_wholesale ) {
    $hash['wpide_wholesale'] = $wpide_is_wholesale() ? 'yes' : 'no';

    return $hash;
} );

Notes

The wholesale role must already exist, and products with an empty wholesale price charge wholesale buyers the normal price.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.