如何添加自定义woocommerce管理字段类型

时间:2016-11-12 02:07:16

标签: woocommerce hook-woocommerce

我已经在woocommerce管理字段中添加了一个自定义字段类型,并且已经100%工作。

但此代码位于woocommerce插件中(woocommerce / includes / admin / class-wc-admin-settings.php)。

我现在的问题是如何从woocoommerce插件中将我的自定义woocommerce管理字段类型排除或挂钩或过滤到我的插件。因此,我的自定义字段类型在更新woocommerce时仍然存在且有效。

这是class-wc-admin-settings.php已自定义

<?php
...
class WC_Admin_Settings {
...

public static function output_fields( $options ) {
...
switch ( $value['type'] ) {
    case 'productcategory' :

    $option_value = (array) self::get_option( $value['id'] );

    ?><tr valign="top">
        <th scope="row" class="titledesc">
            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
            <?php echo $tooltip_html; ?>
        </th>
        <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
            <fieldset>

                <ul class="" style="margin:0; padding:0;">
                <?php
                    $args = array(
                        'orderby' => 'name',
                        'hide_empty'=> 0,
                        'taxonomy' => 'product_cat' 
                    );

                    $all_categories = get_categories( $args );
                    $index = 0;
                    $count = count($all_categories);
                    $numItemsPerRow = ceil($count / 2);
                    $numItemsOffsetFix = $count % 2 == 1;

                    echo '<div class="columns" style="width:auto; display:inline-block; height:auto; float:left; padding:0; margin:0 25px 0 0;">';

                    foreach ($all_categories as $key => $val) {
                        if ($index > 0 and $index % $numItemsPerRow == 0) {
                            echo '</div><div class="columns">';
                            if ($numItemsOffsetFix) {
                                $numItemsPerRow--;
                                $numItemsOffsetFix = false;
                            }
                        }
                    //foreach ( $value['options'] as $key => $val ) {
                        ?>
                        <li style="">
                            <label><input type="checkbox" 
                                name="<?php echo esc_attr( $value['id'] ); ?>[]"
                                id="<?php echo esc_attr( $val->term_id )?>"
                                value="<?php echo esc_attr( $val->term_id )?>" 
                                <?php
                                    if ( in_array( $val->term_id,$option_value ) ) {
                                        echo ' checked="checked"';
                                    }
                                ?>
                                /> <?php echo $val->name; ?>
                            </label>
                        </li>
                        <?php
                        $index++;
                    }
                ?>
                </ul>
            </fieldset>
            <fieldset>
                <?php echo $description; ?>
            </fieldset>
        </td>
    </tr><?php
 break;
...
} //end switch

...
} //end output_fields function

public static function save_fields( $options ) {
...
    switch ( $option['type'] ) {
    ...
        case 'productcategory':
        $value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
        break;
    ...
    }
...
} //end save_fields function

...
} //end class
?>

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

There's a do_action as the default case in the switch statement. Meaning, that if nothing has matched yet in the core code it will see if anything is attached to the action hook. Similarly, you can sanitize via the woocommerce_admin_settings_sanitize_option_$option_name filter

Therefore, here's my best guess:

// handle output of new settings type
add_action( 'woocommerce_admin_field_productcategory', 'output_productcategory_fields' );
function output_productcategory_fields( $value ) {

    $option_value = (array) WC_Admin_Settings::get_option( $value['id'] );

    ?><tr valign="top">
        <th scope="row" class="titledesc">
            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
            <?php echo $tooltip_html; ?>
        </th>
        <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
            <fieldset>

                <ul class="" style="margin:0; padding:0;">
                <?php
                    $args = array(
                        'orderby' => 'name',
                        'hide_empty'=> 0,
                        'taxonomy' => 'product_cat' 
                    );

                    $all_categories = get_categories( $args );
                    $index = 0;
                    $count = count($all_categories);
                    $numItemsPerRow = ceil($count / 2);
                    $numItemsOffsetFix = $count % 2 == 1;

                    echo '<div class="columns" style="width:auto; display:inline-block; height:auto; float:left; padding:0; margin:0 25px 0 0;">';

                    foreach ($all_categories as $key => $val) {
                        if ($index > 0 and $index % $numItemsPerRow == 0) {
                            echo '</div><div class="columns">';
                            if ($numItemsOffsetFix) {
                                $numItemsPerRow--;
                                $numItemsOffsetFix = false;
                            }
                        }
                    //foreach ( $value['options'] as $key => $val ) {
                        ?>
                        <li style="">
                            <label><input type="checkbox" 
                                name="<?php echo esc_attr( $value['id'] ); ?>[]"
                                id="<?php echo esc_attr( $val->term_id )?>"
                                value="<?php echo esc_attr( $val->term_id )?>" 
                                <?php
                                    if ( in_array( $val->term_id,$option_value ) ) {
                                        echo ' checked="checked"';
                                    }
                                ?>
                                /> <?php echo $val->name; ?>
                            </label>
                        </li>
                        <?php
                        $index++;
                    }
                ?>
                </ul>
            </fieldset>
            <fieldset>
                <?php echo $description; ?>
            </fieldset>
        </td>
    </tr><?php

}

// sanitize data for new settings type
add_filter( 'woocommerce_admin_settings_sanitize_option_productcategory', 'sanitize_productcategory_option', 10, 3 );
function sanitize_productcategory_option( $value, $option, $raw_value ){
    $value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
    return $value;
}