我已经为不同产品的变体创建了一个定制字段

add_action('woocommerce_variation_options', 'my_field_variable', 300, 3);

function my_field_variable($loop, $variation_data, $variation){
    woocommerce_wp_text_input(
        array(
            'id' => "_customscu",
            'value' => get_post_meta($variation->ID, '_customscu', true),
            'label' => esc_html__('CustomSCU', 'my_field'),
            'desc_tip' => true,
            'description' => __('Enter the CustomSCU', 'my_field'),
        )
    );
}

在价格之前挂钩WooCommerce_Variation_Options的输出,但我想将其输出到SCU旁边.它是在之前还是之后并不重要.

enter image description here

你能推荐一个能帮我做这件事的钩子吗?

如果没有这样的挂钩,还能怎么做呢?

推荐答案

要在产品WooCommerce Admin Variation(S)中的现有SKU字段附近添加自定义输入字段,因为没有可用的挂钩,您可以在其他任何地方显示该自定义字段,并使用JavaScript将其移动到所需的位置,然后对现有代码进行一些小的更改.

try 以下代码替换(to display the field in the right location and save its value):

// Add the field
add_action('woocommerce_variation_options', 'add_variation_custom_sku_input_field', 300, 3);
function add_variation_custom_sku_input_field($loop, $variation_data, $post){
    $variation = wc_get_product($post->ID);
    $field_key = 'custom_sku';

    echo '<div class="'.$field_key.'-wrapper" data-loop="'.$loop.'">';

    woocommerce_wp_text_input(
        array(
            'id'            => "{$field_key}-{$loop}",
            'name'          => "{$field_key}[{$loop}]",
            'value'         => $variation->get_meta('_'.$field_key),
            'label'         => esc_html__('Custom SKU', 'woocommerce'),
            'desc_tip'      => true,
            'description'   => esc_html__('Enter the custom SKU', 'woocommerce'),
            'wrapper_class' => 'form-row form-row-last',
        )
    );
    echo '</div>';
}

// Display the field in the desired location
add_action( 'admin_footer', 'change_variation_custom_sku_input_field_location' );
function change_variation_custom_sku_input_field_location() {
    global $pagenow, $typenow;

    if( in_array( $pagenow, array('post.php', 'post-new.php') ) && $typenow === 'product' ) :
    
    $field_key = 'custom_sku';
    ?>
    <script>
    jQuery(function($) {
        $('#variable_product_options').on( 'change', function() {
            $('.woocommerce_variable_attributes').each(function(){
                const wrapper = $(this).find('.<?php echo $field_key; ?>-wrapper');
                if ( wrapper.length ) {
                    wrapper.remove();
                    $(this).find('p.variable_sku'+wrapper.data('loop')+'_field').after(wrapper.html());
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

// Save the field inputted value
add_action( 'woocommerce_admin_process_variation_object', 'save_variation_custom_sku_input_field_value', 10, 2 );
function save_variation_custom_sku_input_field_value( $variation, $i ) {
    $field_key = 'custom_sku';

    if( isset($_POST[$field_key][$i]) ) {
        $variation->update_meta_data( '_'.$field_key, sanitize_text_field($_POST[$field_key][$i]) );
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

enter image description here

要水平对齐字段,还需加上the code provided here by the OP即可得到:

enter image description here

Php相关问答推荐

提交表格后的重定向不起作用

composer如何解析包名?

从WooCommerce邮箱通知中的订单详细信息中删除产品列表

保存PHP条带以备将来使用,不显示用户界面

在内部API请求之后,在响应客户端之前,是否忘记了PHP中的Header()?

根据产品类型显示或隐藏WooCommerce管理产品自定义字段

多行PHP属性声明中是否允许单行注释

闭包绑定$This和垃圾收集器

WooCommerce常规价格增加额外的附加平价

WooCommerce中基于用户角色的不同产品差价

Laravel Carbon DiffForHumans选项(年,月,天)

Azure应用程序无法访问共享文件夹

如何在 apache Laragon - laravel 10 中允许方法 DELETE?

Woocommerce 临时购物车税未根据第一个请求正确计算

使用用户元数据填写 woocommerce checkout 字段

当所有对象都属于同一类型时,我可以省略 PHP in_array() 中的 strict 参数吗?

Laravel的Storage::exists返回false,而file_exists返回true.

在 laravel 中更新表单时如何在 Select 选项中保留旧值?

使用 GROUP_CONCAT 规范化/转置来自查询的数据

如何获取最后一条消息属于 Laravel 中特定用户的对话列表