Я добавляю и сохраняю настраиваемое поле в моих переменных продуктах, используя следующие функции:
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' );
function wc_add_custom_fields() {
woocommerce_wp_text_input(
array(
'id' => '_custom_product_pricekg_field',
'label' => __( 'Price per kg', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save' );
function wc_custom_fields_save($post_id)
{
$woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
if (!empty($woocommerce_custom_product_pricekg_field))
update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
Я хотел бы заменить диапазон переменной цены (XX-XX $) на всех страницах архива и продукта с этим настраиваемым полем. Я не знаю, как это сделать. Приведенный ниже фрагмент заменяет диапазон переменной цены переменной по умолчанию, возможно, это можно изменить, чтобы показать значение моего настраиваемого поля?
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}