Настройка поля ACF
Я создал поле выбора в ACF с именем Machine_type со следующими параметрами:
- Грузовик
Строительная машина
Текущий код
Я успешно заполнил описания новых продуктов, используя этот код:
Код: Выделить всё
function prefill_product_description_new() {
global $post;
// Only add pre-filled content for new products
if ($post->post_type == 'product' && $post->post_status == 'auto-draft') {
?>
jQuery(document).ready(function($) {
// Check if the product description is empty and pre-fill it
if ($('#content').val() === '') {
$('#content').val('
This is a default product description for new products.
');
}
// Check if the short description is empty and pre-fill it
if ($('#excerpt').val() === '') {
$('#excerpt').val('This is a default short product description.');
}
});
jQuery(document).ready(function($) {
// Set default content if the description is empty
if ($('#content').val() === '') {
$('#content').val('
This is a default product description for new products.
');
}
// Listen for the change in the machine type dropdown
$('#acf-field_66e994151813').on('change', function() {
var machineType = $(this).val(); // Get the selected machine type
// Check if machine type value is logged correctly
if (machineType === 'Truck') {
$('#content').val('
[b]Truck Description Template[/b]
...');
} else if (machineType === 'Construction Machine') {
$('#content').val('
[b]Construction Machine Description Template[/b]
...');
}
});
});
Подробнее здесь: [url]https://stackoverflow.com/questions/78994807/how-to-pre-fill-woocommerce-product-descriptions-based-on-acf-field-selection[/url]