Предположим, у меня есть переменный продукт с одной вариацией по цене 10 долларов США, а значение настраиваемого поля «базовая цена» равно 200. .
Итак, во внешнем интерфейсе цена этого варианта должна составлять 2000 долларов США (10 долларов США x 200), но в панели администратора цена варианта должна оставаться на уровне 10 долларов США.
Я использовал этот код:
Код: Выделить всё
// 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 );
[img]https://i .sstatic.net/XIejJBsc.png[/img]
Затрагивается только диапазон переменных цен на продукты, я также хочу, чтобы была изменена выбранная цена варианта.
Подробнее здесь: https://stackoverflow.com/questions/784 ... in-woocomm