I have only variable product have custom base price. now I want to change each varation price only in single product page, cart page and checkout page any affect on each orignal price.
Lets assume, I have 1 product and this product have one varation and his varation price is $10 and base price is 200
So, In frontedn I want to show when user is selected this varation price shoud be $2000 but in admin peneal in varation price shoud be 10$.
I am trying this code
// Function to calculate custom price based on variation
function custom_variation_price_html( $price_html, $product ) {
// Check if it's a variable product
if ( $product->is_type( 'variable' ) ) {
// Get all available variations
$variations = $product->get_available_variations();
// Get the base price
$base_price = get_post_meta( $product->get_id(), '_base_price', true );
// Check if base price is set and greater than 0
if ( isset( $base_price ) && $base_price > 0 ) {
foreach ( $variations as $variation ) {
// Get the variation object
$variation_obj = wc_get_product( $variation['variation_id'] );
// Get the regular price of the variation
$regular_price = $variation_obj->get_price();
// Calculate the custom price
$custom_price = floatval( $regular_price ) * $base_price;
// Format the custom price
$formatted_price = wc_price( $custom_price );
// Replace the variation price in the price HTML
$price_html = str_replace( wc_price( $regular_price ), $formatted_price, $price_html );
}
}
}
// Return the modified price HTML
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_variation_price_html', 10, 2 );
this is result I got in above code
I want price override when I change variation.