我正在尝试根据自定义 ACF(高级自定义字段)选择字段动态预填写 WooCommerce 中的产品描述和简短描述。该字段允许用户在两个选项之间进行选择:“卡车”和“建筑机械”。根据选择,我想用特定内容填充产品描述。
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') {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Check if the product description is empty and pre-fill it
if ($('#content').val() === '') {
$('#content').val('<p>This is a default product description for new products.</p>');
}
// Check if the short description is empty and pre-fill it
if ($('#excerpt').val() === '') {
$('#excerpt').val('This is a default short product description.');
}
});
</script>
<?php
}
}
add_action('woocommerce_product_options_general_product_data', 'prefill_product_description_new');
所需功能
我想在创建新产品时根据所选的
post_content
修改post_excerpt
和machine_type
。这是我尝试的代码:
function prefill_product_description_script() {
global $post;
// Make sure this only runs on the product editor screen
if ( $post->post_type == 'product' ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Set default content if the description is empty
if ($('#content').val() === '') {
$('#content').val('<p>This is a default product description for new products.</p>');
}
// 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('<p><strong>Truck Description Template</strong></p><table>...</table>');
} else if (machineType === 'Construction Machine') {
$('#content').val('<p><strong>Construction Machine Description Template</strong></p><table>...</table>');
}
});
});
</script>
<?php
}
}
add_action('admin_footer', 'prefill_product_description_script');
问题
描述未根据所选 ACF 字段预先填写。看来脚本可能无法正确从下拉列表中检索值或在正确的时间执行。
问题
如何确保从 ACF 字段中选择的值正确用于预填写产品描述?
任何帮助将不胜感激!
您应该在打开编辑页面以及字段值发生变化时检查自定义字段的值 试试这个代码
function prefill_product_description_script() {
global $post;
// Make sure this only runs on the product editor screen
if ( $post->post_type == 'product' ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
setDescription();
// Listen for the change in the machine type dropdown
$('#acf-field_66e994151813').on('change', function() {
setDescription();
});
function setDescription(){
var machineType = $('#acf-field_66e994151813').val(); // Get the selected machine type
// Check if machine type value is logged correctly
if (machineType === 'Truck') {
$('#content').val('<p><strong>Truck Description Template</strong></p><table>...</table>');
} else if (machineType === 'Construction Machine') {
$('#content').val('<p><strong>Construction Machine Description Template</strong></p><table>...</table>');
}
}
});
</script>
<?php
}
}
add_action('admin_footer', 'prefill_product_description_script');