In wooCommerce, I have added a custom meta field with a custom price (wholesale price) in edit product pages settings. Everything works well.
When I set a wholesale price it works fine everywhere. But if I try to change this wholesale price it doesn't work. the old price remains everywhere.
What I am doing wrong? How can I solve this problem to allow wholesale price changes?
The code I am using:
function w4dev_get_wholesale_price( $product )
{
if(
$product->is_type( array('simple', 'variable') )
&& get_post_meta( $product->id, '_wholesale_price', true ) > 0
){
return get_post_meta( $product->id, '_wholesale_price', true );
}
elseif(
$product->is_type('variation')
&& get_post_meta( $product->variation_id, '_wholesale_price', true ) > 0
){
return get_post_meta( $product->variation_id, '_wholesale_price', true );
}
return 0;
}
add_action( 'woocommerce_product_options_pricing', 'w4dev_woocommerce_product_options_pricing' );
function w4dev_woocommerce_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_woocommerce_process_product_meta_simple', 10, 1 );
function w4dev_woocommerce_process_product_meta_simple( $product_id )
{
if( isset($_POST['_wholesale_price']) && $_POST['_wholesale_price'] > 0 ){
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
}
add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_get_price', 10, 2);
function w4dev_woocommerce_get_price( $price, $product )
{
if( w4dev_get_wholesale_price($product) > 0 ) {
$price = w4dev_get_wholesale_price($product);
return $price;
}
}
See Question&Answers more detail:os