我将自定义字段添加到产品变体(following this guide. 它工作得很好,但它将自定义字段添加到所有产品. 我只想添加自定义字段到特定的产品及其变体. 例如:如果产品ID等于‘1234’,则WP会将自定义字段添加到其变体中.

我try 在unction.php中处理此问题:

global $post;      
$product_id = $post->ID; 
$product_array = array( 1234, 9999 );
if ( in_array( $product_id, $product_array )) {
    // add custom field
}

...但它只适用于产品,而不适用于其变体.

有可能吗? 非常感谢!

推荐答案

要针对特定的变量产品(及其所有变体),您可以使用post_parent字段从第$variations个第三个函数参数(the WP_Post object of the current variation)中直接获得父变量产品ID.

因此,您将改用:

$targeted_ids = array( 1234, 9999 ); // Targeted variable products IDs

if ( in_array( $variation->post_parent, $targeted_ids )) {
    // add a custom setting field
}

下面是一个带有输入文本字段的完整代码示例:

// Add custom field(s) to product variations from specific variable products
add_action( 'woocommerce_product_after_variable_attributes', 'add_variation_setting_fields', 10, 3 );
function add_variation_setting_fields( $loop, $variation_data, $variation ) {
    $targeted_ids = array( 1234, 9999 ); // Targeted variable product Ids

    if ( ! in_array( $variation->post_parent, $targeted_ids ) ) return;

    $field_key1 = 'my_custom_field';
    
    woocommerce_wp_text_input( array(
        'id'            => $field_key1.'['.$loop.']',
        'label'         => __('Text field Name', 'woocommerce'),
        'wrapper_class' => 'form-row',
        'placeholder'   => __('Text field placeholder', 'woocommerce'),
        'description'   => __('Text field description', 'woocommerce'),
        'desc_tip'      => true,
        'value'         => get_post_meta($variation->ID, $field_key1, true)
    ) );
}

// Save the custom field from product variations
add_action('woocommerce_admin_process_variation_object', 'save_variation_setting_fields', 10, 2 );
function save_variation_setting_fields($variation, $i) {
    $field_key1 = 'my_custom_field';

    if ( isset($_POST[$field_key1][$i]) ) {
        $variation->update_meta_data($field_key1, sanitize_text_field($_POST[$field_key1][$i]));
    }
}

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


对于多个输入文本字段:

您可以使用以下优化代码版本(where the fields settings are in a reusable custom function):

// Fields settings
function custom_fields_settings() {
    return array(
        'my_custom_field1' => array(
            'label'       => __('Text field Name 1', 'woocommerce'),
            'placeholder' => __('Text field placeholder 1', 'woocommerce'),
            'description' => __('Text field description 1', 'woocommerce'),
        ),
        'my_custom_field2' => array(
            'label'       => __('Text field Name 2', 'woocommerce'),
            'placeholder' => __('Text field placeholder 2', 'woocommerce'),
            'description' => __('Text field description 2', 'woocommerce'),
        ),
        'my_custom_field3' => array(
            'label'       => __('Text field Name 3', 'woocommerce'),
            'placeholder' => __('Text field placeholder 3', 'woocommerce'),
            'description' => __('Text field description 3', 'woocommerce'),
        ),
    );
}


// Add custom field(s) to product variations from specific variable products
add_action( 'woocommerce_product_after_variable_attributes', 'add_variation_setting_fields', 10, 3 );
function add_variation_setting_fields( $loop, $variation_data, $variation ) {
    $targeted_ids = array( 1234, 9999 ); // Targeted variable product Ids

    if ( ! in_array( $variation->post_parent, $targeted_ids ) ) return;

    foreach(  custom_fields_settings() as $field_key => $values ) {
        $args = array(
            'id'            => $field_key.'['.$loop.']',
            'wrapper_class' => 'form-row',
            'desc_tip'      => true,
            'value'         => get_post_meta($variation->ID, $field_key, true)
        );
        woocommerce_wp_text_input( array_merge( $args, $values) );
    }
}

// Save the custom field from product variations
add_action('woocommerce_admin_process_variation_object', 'save_variation_setting_fields', 10, 2 );
function save_variation_setting_fields($variation, $i) {
    foreach(  custom_fields_settings() as $field_key => $values ) {
        if ( isset($_POST[$field_key][$i]) ) {
            $variation->update_meta_data($field_key, sanitize_text_field($_POST[$field_key][$i]));
        }
    }
}

Php相关问答推荐

获得客户S在WooCommerce为期1年的总支出

如果WooCommerce中存在特定的运输方式,则隐藏其他运输方式

在WooCommerce中添加基于类别的固定费用

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

如何将WooCommerce产品属性术语名称显示为链接术语?

Chillerlan/php-iOS名称字段的QR vCard错误

模式 bootstrap 未显示

如何按类别过滤自定义帖子类型

在WooCommerce购物车页面应用优惠券不会';t刷新总计

重命名 WordPress 中现有的自定义分类法名称和标签

按日期向 WooCommerce 中的我的帐户订单添加过滤器

安装 Herd 并返回 Valet 后为 502

Woocommerce注册中的自定义复选框验证错误提示问题

在WooCommerce可变产品中显示所选变体的自定义字段

PHP使用三元运算符进行null合并(再次)

ACF 更新字段功能不更新 WordPress 中的任何数据

如何从 PHP 中的 .txt 文件中列出今天生日的人的姓名?

按数字然后按字符串对关联 PHP 数组进行排序

PHP Remedy API 调用以创建附件无效的条目(使用 Postman Works!)

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表