在Woocommerce admin产品变体中添加基于产品类别的下拉列表 [英] Add a dropdown based on product categories in Woocommerce admin product variation

查看:57
本文介绍了在Woocommerce admin产品变体中添加基于产品类别的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to replace checkboxes fields with a unique select field to display product categories term names that I get from a child term ID, in admin product variation settings (for my variable products only).

因此, woocommerce_wp_checkbox()函数将替换为 woocommerce_wp_select().

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

Here is my WORKING code for checkboxes:

<?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 );
    }
}
?>

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

How can I replace the checkboxes by a dropdown (OR eventually radio buttons)?

我们非常感谢您的帮助.

Any help is really appreciated.

推荐答案

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

In your actual code there is some mistakes like using get_categories() to get product category custom taxonomy terms. Instead just use get_terms()

以下代码将启用一个选择字段,而不是多个复选框.选择值保存正确.

The below code will enable a select field instead of multiple checkboxes. The select value is well saved.

重新访问的代码:

// 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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce admin产品变体中添加基于产品类别的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆