I am trying to add a wholesale price on my Variable products but I wonder if I can keep the same field as for my simple products. The code I have for my simple products is this:
/* Custom user roles */
add_role( 'wholesaler', 'Wholesaler', get_role( 'customer' )->capabilities );
add_action( 'woocommerce_product_options_pricing', 'action_woocommerce_product_options_pricing', 10, 0 );
/* Add custom wholesaler price field to general page */
function action_woocommerce_product_options_pricing() {
woocommerce_wp_text_input( array(
'id' => 'wholesaler_price',
'class' => 'wc_input_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
) );
}
// Save Fields
function action_woocommerce_admin_process_product_object( $product ) {
if( isset($_POST['wholesaler_price']) ) {
$product->update_meta_data( 'wholesaler_price', sanitize_text_field( $_POST[ 'wholesaler_price'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)
function custom_price_assign( $price, $product ) {
// Check if the user has a role of wholesaler
if ( current_user_can('wholesaler') && $wholesaler_price = $product->get_meta('wholesaler_price') ){
return $wholesaler_price;
}
return $price;
}
My code is influenced and taken from this as the other user wanted to achieve the same. But now I need this to be available as a field on my variable products. Is it possible to keep the same field for the variable products as well?