Woocommerce product category fields
Yeah, we step into Woocommerce chambers. This is a huge, good plugin, widely recognizable, used on many many e-commerce sites. They got nice product category taxonomy and it works fine. But what if we need a few more fields? It’s not that hard as you may think so, because Woocommerce is well written plugin, we can hook into it without any trouble.
We need four action hooks and three functions. I will use methods, because I prefer object oriented programming, but it’s pretty easy to convert it into functional programming. You may ask, why only three functions, when we want to use four hooks? Two hooks will be handled by one function. All right, let’s build our class. We will call it ProductCategory
and will put it into \Phylax\WordPress\Theme\Library
namespace to avoid any conflicts.
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Phylax\WordPress\Theme\Library; class ProductCategory { public function product_cat_add_form_fields(){ } public function product_cat_edit_form_fields(){ } public function create_edit_update( $term_id ){ } } |
How to call this class? Well, use
functions.php
file of your theme of course.
1 2 3 4 5 |
$phylax_product_category = new \Phylax\WordPress\Theme\Library\ProductCategory; add_action( 'product_cat_add_form_fields', array( $phylax_product_category, 'product_cat_add_form_fields' ) ); add_action( 'product_cat_edit_form_fields', array( $phylax_product_category, 'product_cat_edit_form_fields' ) ); add_action( 'edited_product_cat', array( $phylax_product_category, 'create_edit_update' ) ); add_action( 'create_product_cat', array( $phylax_product_category, 'create_edit_update' ) ); |
And that’s it. This will work as soon as we add some fields… What field would you like to add? Let’s do something very simple at the beginning. Let’s add “External link” field for, well, external link for every category. We may need it for many reasons, you can figure out your idea why exactly. Let’s rebuild our class file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php namespace Phylax\WordPress\Theme\Library; class ProductCategory { public $fields = array( 'external-link' => array( 'slug' => 'external-link', 'label' => __( 'External link', 'text-domain' ), 'description' => __( 'Why do we need external link and where will we put it?', 'text-domain' ), ), ); public function product_cat_add_form_fields(){ foreach( $this->fields as $item ) { ?> <div class="form-field"> <label for="term_meta[<?=$item['slug']?>]"><?=$item['label']?></label> <input type="text" name="term_meta[<?=$item['slug']?>]" id="term_meta[<?=$item['slug']?>]" value=""> <p class="description"><?=$item['description']?></p> </div> <?php } } public function product_cat_edit_form_fields(){ if ( !isset( $_POST['term_meta'] ) ) { return; } $term_meta = get_option( 'taxonomy_' . $term_id ); foreach( $this->fields as $item ) { ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[<?=$item['slug']?>]"><?=$item['label']?></label></th> <td> <input type="text" name="term_meta[<?=$item['slug']?>]" id="term_meta[<?=$item['slug']?>]" value="<?php echo esc_attr( $term_meta[ $item['slug'] ] ) ? esc_attr( $term_meta[ $item['slug'] ] ) : ''; ?>"> <p class="description"><?=$item['description']?></td> </tr> <?php } } public function create_edit_update( $term_id ){ if ( !isset( $_POST['term_meta'] ) ) { return; } $term_meta = get_option( 'taxonomy_' . $term_id ); $term_keys = array_keys( $_POST['term_meta'] ); foreach ( $term_keys as $key ) { if ( isset ( $_POST[ 'term_meta' ][ $key ] ) ) { $term_meta[$key] = esc_attr( $_POST[ 'term_meta' ][ $key ] ); } } update_option( 'taxonomy_' . $term_id, $term_meta ); } } |
Note, that I may did some typos here, I wrote it here, in WordPress editor, so do not feel angry when something isn’t right. Please feel free to post comments about it and I will certainly fix any mistakes.
2 Comments
Ronald Mitterer
I think there is something wrong in function product_cat_edit_form_fields? The variable $term_meta is NULL? I added:
$term_meta = get_option( ‘taxonomy_’ . $term->term_id );
before the for-loop and now it works.
Please correct me, if I’m wrong.
Łukasz Nowicki
Ronald, you are absolutely right, thank you for noticing. As I wrote in the post, I wrote this code in WP editor, so I didn’t test it. Of course it should get option first, like it did in
create_edit_update
method. I corrected it, thank you again.