在 Woocommerce 管理产品变体中添加基于产品类别的下拉菜单

Add a dropdown based on product categories in Woocommerce admin product variation

我需要用一个唯一的 select 字段替换复选框字段,以显示我从子术语 ID 获得的产品类别术语名称,在管理产品变体设置中(仅适用于我的可变产品)。

因此 woocommerce_wp_checkbox() 函数将被 woocommerce_wp_select() 代替。

这是我的复选框工作代码:

<?php
// Woocommerce Product meta
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
// Add fields
function variation_settings_fields( $loop, $variation_data, $variation ) {  
    // Characteristics
    $args = array( 'type' => 'product', 'taxonomy' => 'product_cat', 'child_of' => 20 ); 
    $categories = get_categories( $args );
    foreach ($categories as $cat) {
        woocommerce_wp_checkbox( array( 
            "id"            => $cat->name .'_['. $variation->ID .']', 
            "label"         => __(" " . $cat->name, "woocommerce" ), 
            "value"         => get_post_meta( $variation->ID, $cat->name .'_', true ), 
            )
        );
    }

    ?>
<?php
}
// Save
function save_variation_settings_fields( $post_id ) {
    // Characteristics
    $args = array( 'type' => 'product', 'taxonomy' => 'product_cat', 'child_of' => 20 ); 
    $categories = get_categories( $args );
    foreach ($categories as $cat) {
        $checkbox = isset( $_POST[$cat->name . '_'][ $post_id ] ) ? 'yes' : 'no';
        update_post_meta( $post_id, $cat->name . '_', $checkbox );
    }
}
?>

如何用下拉菜单 (或最后的单选按钮) 替换复选框?

非常感谢任何帮助。

在您的实际代码中存在一些错误,例如使用 get_categories() 获取产品类别自定义分类术语。相反,只需使用 get_terms()

下面的代码将启用 select 字段而不是多个复选框。 select值保存的很好

重访代码:

// Add Variation settings custom field
add_action( 'woocommerce_product_after_variable_attributes', 'add_product_category_variation_field', 11, 3 );
function add_product_category_variation_field( $loop, $variation_data, $variation ) {
    // Get product categories that are child of term = 20
    $terms   = get_terms( array('taxonomy' => 'product_cat', 'child_of' => 20 ) );
    $options = []; // Initializing

    // Loop through each wp_term object and set term names in an array
    foreach ($terms as $term) {
        $term_name = __( $term->name, "woocommerce" );
        $options[$term_name] = $term_name;
    }

    // The select field
    woocommerce_wp_select( array(
        'id'            => '_product_category',
        'name'          => "_product_category_$loop",
        'label'         => __("product categories", "woocommerce" ),
        'options'       => $options,
        'value'         => get_post_meta( $variation->ID, '_product_category', true ),
    ) );
}

// Save Variation settings custom field
add_action( 'woocommerce_save_product_variation', 'save_product_category_variation_field', 11, 2 );
function save_product_category_variation_field( $variation_id, $loop ){
    if( isset($_POST["_product_category_$loop"]) )
        update_post_meta( $variation_id, '_product_category', esc_attr( $_POST["_product_category_$loop"] ) );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。