wordpress主题WooCommerce 添加商品分类自定义字段

WooCommerce 是最流行的 wordpress 电子商务插件,在开发 WooCommerce 主题的时候,难免要添加一些自定义字段来满足我们的需求。WooCommerce 是基于 WordPress 的生态系统开发的,所以,很多 WordPress 的 API 我们都可以直接用在 WooCommerce 中。下面我们以一个为分类添加字体图标的需求来讲解一下怎么为 WooCommerce 的商品分类添加自定义字段,效果如下。

添加表单到商品分类添加页面

首先是商品分类添加页面,我们添加一个图标的表单,用户填写图标的名称,我是用的字体图标,当然,也可以是图片网址。这里我们使用 WooCommerce 自定义的 product_cat_add_form_fields 来把表单添加到商品分类添加页面。

// 分类添加表单
function tutorialshares_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="term_meta[term_icon]"><?php _e( '图标', 'woocommerce' ); ?></label>
        <input class="regular-text" type="text" name="term_meta[term_icon]" id="term_meta[term_icon]" value="">
    </div>
<?php
}
add_action( 'product_cat_add_form_fields', 'tutorialshares_taxonomy_add_new_meta_field', 10, 2 );

添加表单到商品分类编辑页面

商品分类编辑页面用的是 product_cat_edit_form_fields,和上面的基本上是差不多的,只不过多了已经添加过的数据,其实两个是可以合并成一个功能的。

// 分类编辑表单
function tutorialshares_taxonomy_edit_meta_field($term) {
    // put the term ID into a variable
    $t_id = $term->term_id;
 
    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[term_icon]"><?php _e( '图标', 'woocommerce' ); ?></label></th>
        <td>
            <input class="regular-text" type="text" name="term_meta[term_icon]" id="term_meta[term_icon]" value="<?php echo esc_attr( $term_meta['term_icon'] ) ? esc_attr( $term_meta['term_icon'] ) : ''; ?>">
        </td>
    </tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'tutorialshares_taxonomy_edit_meta_field', 10, 2 );

最后,我们需要把提交的数据保存到数据库中

WooCommercce 直接把商品分类字段保存在了 WordPress 的 wp_options 数据表中,个人觉得这是一个很好的设计,因为商品分类的自定义字段一般不会很多,专门为此新建一个数据表就增加了很多复杂度,WordPress 的 wp_options 基本上是 k-v 性质的,也很适合自定义字段这种类型的数据。WordPress 也提供了很方便的功能接口,直接用 update_option 保存数据即可,

// 保存分类数据
function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}  
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

调用商品分类自定义字段数据

从上面保存数据的代码中可以看出来。WooCommerce 把商品分类的自定义字段保存到 wp_options 数据表中了,调用这个数据的时候,我们直接用 WordPress 的 get_option 函数就可以了。

$meta_field_array = get_option('taxonomy_'. $term->term_id);
$meta_icon = $meta_field_array['term_icon'];

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » wordpress主题WooCommerce 添加商品分类自定义字段