Из поля установки дополнительной цены товара (см. Скриншоты ниже) , минимальная цена за единицу товара заменяет отображаемую цену товара в магазине Woocommerce, архивах и на страницах с отдельным товаром.
На основе " Установите минимальную отображаемую цену для переменных продуктов в Woocommerce " код ответа, следующее будет работать для простых и переменных продуктов , которые будутиспользуется с Woocommerce 3 и выше .
Код (полностью пересмотрен и улучшен):
// Backend: Add and display a custom field for simple and variable products
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_price_field_to_general_product_data' );
function add_custom_price_field_to_general_product_data() {
global $product_object;
echo '<div class="options_group hide_if_external">';
woocommerce_wp_text_input(array(
'id' => '_min_unit_price',
'label' => __('Min Unit price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
'description' => __('Enter the minimum unit price here.', 'woocommerce'),
'desc_tip' => 'true',
'value' => str_replace('.', ',', $product_object->get_meta('_min_unit_price') ),
'data_type' => 'price'
));
echo '</div>';
}
// Backend: Save the custom field value for simple and variable products
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_price_field', 10, 1 );
function save_product_custom_price_field( $product ) {
if ( isset($_POST['_min_unit_price']) ) {
$product->update_meta_data( '_min_unit_price', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_min_unit_price'] ) ) ) );
}
}
// Frontend variable products: Display the min price with "From" prefix label
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 10, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {
if( $min_unit_price = $product->get_meta('_min_unit_price') ){
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
$price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
}
return $price;
}
// Frontend simple products: Display the min price with "From" prefix label
add_filter( 'woocommerce_get_price_html', 'custom_min_unit_product_price_html', 10, 2 );
function custom_min_unit_product_price_html( $price, $product ) {
if( $product->is_type('simple') && $min_unit_price = $product->get_meta('_min_unit_price') ){
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
$price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
}
return $price;
}
Код помещается в файл function.php вашей активной дочерней темы (или активная тема).Проверено и работает.
В бэкэнде, для простых и переменных продуктов:


Во внешнем интерфейсе, для простых и переменных продуктов (в магазине, в архивах и на страницах отдельных продуктов)
