Assign custom field price as product price to specific user role in WooCommerce more than one

33 Views Asked by At

your textAssign custom field price as product price to specific user role in WooCommerce (more than one) i used above code i found at stack overflow

I created a new role called "_2". (from plug in) The role works fine

I also created a custom price field called "Wholesaler price2". It also works fine

how can i make more // woocommerce_product_options_pricing id, label to work ?

how can i make more woocommerce_product_options_pricing id, label to work ? My functions.php code i used so far is below:your text

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_price2', 
        'class' => 'short wc_input_price', 
        'label' => __( 'Χονδρική 2', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
    ) );
}

    

// Save Fields
function action_woocommerce_admin_process_product_object( $product ) {
    if( isset($_POST['wholesaler_price2']) ) {
        $product->update_meta_data( 'wholesaler_price2', sanitize_text_field( $_POST[ 'wholesaler_price2'] ) );
    }
}
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('_2') && $wholesaler_price2 = $product->get_meta('wholesaler_price2') ){
        return $wholesaler_price2;
    }
    return $price;
}

0

There are 0 best solutions below